Skip to content

Instantly share code, notes, and snippets.

View whitelizard's full-sized avatar
💚
Focusing

Esbjörn Blomquist whitelizard

💚
Focusing
View GitHub Profile

Keybase proof

I hereby claim:

  • I am whitelizard on github.
  • I am whitelizard (https://keybase.io/whitelizard) on keybase.
  • I have a public key ASDQQj8ThUyAUGANASXBwYlA-tT0kJ4sMe5qFwRZSvs8-Ao

To claim this, I am signing this object:

@whitelizard
whitelizard / promise-race-util-1.js
Last active October 30, 2019 09:18
Promise.race Util 1
const triggerPromise = () => {
let resolve;
const promise = new Promise(r => (resolve = r));
return [promise, resolve];
};
@whitelizard
whitelizard / safer-code-es6-destructuring-1.js
Created October 30, 2019 12:56
Safer Code ES6 Destructuring 1
// item and items are predefined
const parent = items[item.parent];
const types = parent.types;
const name = parent.name;
@whitelizard
whitelizard / safer-code-es6-destructuring-2.js
Created October 30, 2019 12:57
Safer Code ES6 Destructuring 2
const parentId = item.parent;
const parent = parentId ? (items[parentId] || {}) : {};
const types = parent.types || [];
const name = parent.name || 'Noname';
@whitelizard
whitelizard / safer-code-es6-destructuring-3.js
Created October 30, 2019 12:57
Safer Code ES6 Destructuring 3
const { parent: parentId } = item;
const { [parentId]: { types = [], name = 'Noname' } = {} } = items;
@whitelizard
whitelizard / oop-to-fp-class.js
Created November 18, 2019 23:39
Traditional OOP style class in JavaScript
class Circle {
constructor(pos = [0, 0], r = 0) {
this.pos = pos;
this.radius = r;
this.hitCount = 0;
}
get r() {
return this.radius;
}
@whitelizard
whitelizard / oop-to-fp-class-tests.js
Last active November 18, 2019 23:40
Traditional OOP style class tests
const c1 = new Circle([2.5, 0], 1.5);
const c2 = new Circle([-2, 0], 2);
c1.area(); // -> 7.0685834705770345
c2.area(); // -> 12.566370614359172
c1.intersect(c2); // -> false
c1.hits; // -> 0
c1.pos[0] = 1;
@whitelizard
whitelizard / oop-to-fp-constructor-tests.js
Created November 18, 2019 23:42
Constructor pattern tests
const c1 = new Circle([2.5, 0], 1.5);
const c2 = new Circle([-2, 0], 2);
c1.area(); // -> 7.0685834705770345
c2.area(); // -> 12.566370614359172
c1.intersect(c2); // -> false
c1.getHits(); // -> 0
c1.pos[0] = 1;
@whitelizard
whitelizard / oop-to-fp-factory-tests.js
Created November 18, 2019 23:44
Factory pattern tests
const c1 = makeCircle([2.5, 0], 1.5);
const c2 = makeCircle([-2, 0], 2);
c1.area(); // -> 7.0685834705770345
c2.area(); // -> 12.566370614359172
c1.intersect(c2); // -> false
c1.getHits(); // -> 0
c1.pos[0] = 1;
@whitelizard
whitelizard / oop-to-fp-immutable-store-reducer.js
Created November 18, 2019 23:48
Reducer for Immutable Store in JavaScript
const reducer = (state, action) => {
const actions = {
move: ({ id, payload }) => ({ ...state, [id]: setPos(state[id], payload) }),
hit: ({ id1, id2 }) => ({
...state,
[id1]: incHits(state[id1]),
[id2]: incHits(state[id2]),
}),
};
return actions[action.type](action);