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 / 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-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-constructor.js
Last active December 17, 2019 21:37
Constructor pattern in JavaScript
function Circle(pos = [0, 0], r = 0) {
this.pos = pos;
let hits = 0;
this.getR = () => r;
this.getHits = () => hits;
this.incHits = () => (hits += 1);
this.area = () => r ** 2 * Math.PI;
this.intersect = circle => {
@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-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 / 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 / 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-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 / 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)
),
]);