Skip to content

Instantly share code, notes, and snippets.

View jsdf's full-sized avatar

James Friend jsdf

View GitHub Profile
@jsdf
jsdf / UsernameField.js
Created August 2, 2015 22:57
Private state in React Components
let UsernameField = (() => {
const _username = Symbol();
const _handleNameChange = Symbol();
return class UsernameField extends React.Component {
state = {
[_username]: 'DefaultUser',
}
[_handleNameChange] = (e) => {
@jsdf
jsdf / this.js
Created August 7, 2015 03:24
Understanding 'this' in Javascript
// A useful way of thinking about how the value of 'this' is bound in Javascript
// functions is to imagine that Function.prototype.call is being used
// implicitly at the call site to set the 'context' (the 'this' value). eg.
// assuming in each case
var o = {}
var o2 = {}
var fn = function(){}
// 1. bare function call
@jsdf
jsdf / shallowRender.js
Created September 7, 2015 06:44
React Shallow Rendering helper
import React from 'react/addons';
export default function shallowRender(element) {
const shallowRenderer = React.addons.TestUtils.createRenderer();
shallowRenderer.render(element);
return shallowRenderer.getRenderOutput();
}
@jsdf
jsdf / recursiveReactChildrenToArray.js
Created September 8, 2015 23:32
Utility function to walk react element children and expand to an array of every element in tree (without removing children from elements)
import React from 'react';
export default function flattenReactChildrenToArray(nodeChildren, accumulated = []) {
React.Children.forEach(nodeChildren, (childNode) => {
accumulated.push(childNode);
if (childNode && childNode.props && childNode.props.children) {
flattenReactChildrenToArray(childNode.props.children, accumulated);
}
});
return accumulated;
@jsdf
jsdf / immutableJSFormatter.js
Last active May 19, 2022 10:51
Chrome devtools formatter for Immutable-js
const immutableJSFormatter = {
header(x) {
if (x && x.toJS) return ['span', {}, x.toString()];
return null;
},
hasBody(x) {
return x && x.toJS;
},
body(x) {
return ['span', {}, JSON.stringify(x.toJS(), null, 2)];
@jsdf
jsdf / setupTestFramework.js
Created November 5, 2015 09:27
Make React PropType warnings throw errors in Jasmine/Jest
var util = require('util');
// nobody cares about warnings so lets make them errors
// keep a reference to the original console methods
var consoleWarn = console.warn;
var consoleError = console.error;
function logToError() {
throw new Error(util.format.apply(this, arguments).replace(/^Error: (?:Warning: )?/, ''));
@jsdf
jsdf / __tests__getStuff-test.js
Last active August 30, 2020 20:02
Writing and testing async JS with Jest, promises and async functions
jest.dontMock('../getStuff');
describe('getStuff', () => {
let getStuff;
let request;
let stuffStore;
it('loads the data', () => {
const id = 1;
const data = {a: 1};
@jsdf
jsdf / Fetch-API-cheat-sheet.md
Last active August 18, 2022 20:23
Fetch API cheat sheet

fetch api cheat sheet

get JSON

fetch('/things/10', {
  credentials: 'same-origin',
  headers: {
    'accept': 'application/json'
  }
@jsdf
jsdf / imageProcessing.js
Last active February 7, 2019 04:22
utils to break images into tiles, crop and flip them
var gm = require('gm');
var glob = require('glob');
var path = require('path');
var fs = require('fs');
const labels = [];
function pathWithSuffix(inpath, suffix) {
return path.join(path.dirname(inpath),path.basename(inpath,'.jpg')+suffix+'.jpg');
}
@jsdf
jsdf / pce-config.cfg
Created May 2, 2020 21:17
pce emulator configuration file disk example
sony {
enable = 1
# The floppy disk insertion delay. Set this to:
# 0: never automatically insert the disk
# 1: have the disk inserted immediately on startup.
# The System will then either boot from the disk or
# eject it.
# x: delay insertion by x (emulated) seconds.