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 / promise-race-test-3.js
Last active November 6, 2021 16:38
Promise.race Test 3
describe('publish/subscribe 3', () => {
it('should send a message from publish to subscribe', async () => {
let result;
const [promise, resolve] = triggerPromise();
subscribe('ch1', (msg) => {
result = msg;
resolve();
});
publish('ch1', 1);
await asyncWithTimeout(promise);
@whitelizard
whitelizard / promise-race-util-2.js
Last active November 6, 2021 16:37
Promise.race Util 2
const asyncWithTimeout = (promise, timeout = 900) =>
Promise.race([
promise,
new Promise((_, reject) =>
setTimeout(() => reject(Error('Async timeout')), timeout)
),
]);
@whitelizard
whitelizard / promise-race-test-2.js
Last active November 6, 2021 16:36
Promise.race Test 2
describe('publish/subscribe 2', () => {
it('should send a message from publish to subscribe', async () => {
let result;
const [promise, resolve] = triggerPromise();
subscribe('ch1', (msg) => {
result = msg;
resolve();
});
publish('ch1', 1);
await promise;
@whitelizard
whitelizard / promise-race-test-1.js
Last active November 6, 2021 16:35
Promise.race Test 1
describe('publish/subscribe 1', () => {
it('should send a message from publish to subscribe', async () => {
let result;
subscribe('ch1', (msg) => (result = msg));
publish('ch1', 1);
await wait(900);
expect(result).toBe(1);
});
});
@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 / 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 / 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.js
Last active June 17, 2020 12:24
Factory pattern in JavaScript
function makeCircle(pos = [0, 0], r = 0) {
let hits = 0;
const obj = {
pos,
getR: () => r,
getHits: () => hits,
incHits: () => (hits += 1),
area: () => r ** 2 * Math.PI,
intersect: (circle) => {
@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());