Skip to content

Instantly share code, notes, and snippets.

View whitelizard's full-sized avatar
💚
Focusing

Esbjörn Blomquist whitelizard

💚
Focusing
View GitHub Profile
@whitelizard
whitelizard / destructuring-possibilities.js
Last active September 26, 2021 17:27
Destructuring possibilities
const func = (
{
fallbackMessage: msg = 'Unknown',
offset = 4,
pos: { x = 0, y = 0 } = {},
...options
} = {},
data = {},
) => {
const { title = '', items: [head, ...tail] = [] } = data;
@whitelizard
whitelizard / destructuring-multiple-steps.js
Last active September 26, 2021 17:29
Destructuring in multiple steps
const func = (params = {}, data = {}) => {
const {
fallbackMessage: msg = 'Unknown',
offset = 4,
pos = {},
...options
} = params;
const { x = 0, y = 0 } = pos;
const { title = '', items = [] } = data;
@whitelizard
whitelizard / oop-to-fp-class-reuseability-1.js
Created November 19, 2019 22:11
Reuse Function in a Class
Circle.prototype.area.bind({ r: 4 })()
@whitelizard
whitelizard / oop-to-fp-immutable-store-tests.js
Created November 18, 2019 23:49
Functional style Immutable Store tests
const initialState = {
1: circle([2.5, 0], 1.5),
2: circle([-2, 0], 2),
};
const { getState, dispatch } = createStore(initialState, reducer);
area(getState(1).r); // -> 7.0685834705770345;
area(getState(2).r); // -> 12.566370614359172;
const [hit] = intersect(getState(1), getState(2));
hit; // -> false
@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);
@whitelizard
whitelizard / oop-to-fp-immutable-store.js
Last active December 22, 2019 15:24
Functional Style with Immutable Store in JavaScript
const deepFreeze = item => {
const complex = Object(item) === item;
if (Object.isFrozen(item) || !complex) return item;
Object.values(item).forEach(deepFreeze);
return Object.freeze(item);
};
const createStore = (initState = {}, reducer) => {
let state = deepFreeze(initState);
return {
@whitelizard
whitelizard / oop-to-fp-functional-factory.js
Last active December 20, 2019 21:01
Functional style Factory in JavaScript
const makeCircle = (pos = [0, 0], r = 0) => {
let state = circle(pos, r);
return {
getState: () => state,
incHits: () => (state = incHits(state)),
setPos: pos => (state = setPos(state, pos)),
area: () => area(r),
intersect: c2 => {
let hit;
[hit, state] = intersect(state, c2.getState());
@whitelizard
whitelizard / oop-to-fp-functional-style-tests.js
Last active December 20, 2019 20:58
Functional style programming tests
let c1 = circle([2.5, 0], 1.5);
let c2 = circle([-2, 0], 2);
const c2ref = c2; // Use a reference to c2 at this point in time
area(c1.r); // -> 7.0685834705770345
area(c2.r); // -> 12.566370614359172
let hit;
[hit, c1, c2] = intersect(c1, c2);
hit; // -> false
@whitelizard
whitelizard / oop-to-fp-functional-style.js
Last active June 17, 2020 18:02
Functional style programming in JavaScript
// Circle data object creator
const circle = (pos = [0, 0], r = 0) => ({ pos, r, hits: 0 });
// Immutable-updates helpers
const incHits = obj => ({ ...obj, hits: (obj.hits || 0) + 1 });
const setPos = (obj, pos = [0, 0]) => ({ ...obj, pos });
// Circle computations
const area = r => r ** 2 * Math.PI;
@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;