Skip to content

Instantly share code, notes, and snippets.

View ericelliott's full-sized avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
@ericelliott
ericelliott / no-timing-dependency.js
Last active April 4, 2022 20:01
Pure functions have no timing dependency issues.
const x = {
val: 2
};
const x1 = x => Object.assign({}, x, { val: x.val + 1});
const x2 = x => Object.assign({}, x, { val: x.val * 2});
console.log(x1(x2(x)).val); // 5
@ericelliott
ericelliott / impure-action-creator.js
Last active March 28, 2022 21:58
Impure Action Creator
// Action creators can be impure.
export const addChat = ({
// cuid is safer than random uuids/v4 GUIDs
// see usecuid.org
id = cuid(),
msg = '',
user = 'Anonymous',
timeStamp = Date.now()
} = {}) => ({
type: ADD_CHAT,
@ericelliott
ericelliott / summing-reducer.js
Last active March 28, 2022 21:30
Summing reducer
const defaultState = 0;
const reducer = (state = defaultState, action) => {
switch (action.type) {
case 'ADD': return state + action.payload;
default: return state;
}
};
@ericelliott
ericelliott / summing-reducer-test-actions.js
Last active March 28, 2022 21:29
Testing the summing reducer
const actions = [
{ type: 'ADD', payload: 0 },
{ type: 'ADD', payload: 1 },
{ type: 'ADD', payload: 2 }
];
const total = actions.reduce(reducer, 0); // 3
@ericelliott
ericelliott / passing-values-to-generators.js
Created May 20, 2016 07:14
Passing values into generators through `.next()`
function* crossBridge() {
const reply = yield 'What is your favorite color?';
console.log(reply);
if (reply !== 'yellow') return 'Wrong!'
return 'You may pass.';
}
{
const iter = crossBridge();
const q = iter.next().value; // Iterator yields question
import test from 'tape';
const before = test;
const after = test;
// beforeEach/afterEach rely on shared state.
// That's a big anti-pattern for testing.
// It's also silly to run something before and after
// ever test -- many of your tests won't need it.
@ericelliott
ericelliott / dev-vs-production-type-checking.js
Last active December 7, 2021 22:33
Dev vs production type checking
const check = fn => (params = []) => {
const { required } = fn;
const missing = required.filter(param => !(param in params));
if (missing.length) {
throw new Error(`${ fn.name }() Missing required parameter(s):
${ missing.join(', ') }`);
}
return fn(params);
@ericelliott
ericelliott / compose.js
Created September 6, 2016 00:34
Compose implementation
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);
const getSecret = (secret) => {
return {
get: () => secret
};
};
test('Closure for object privacy.', assert => {
const msg = '.get() should have access to the closure.';
const expected = 1;
const obj = getSecret(1);
@ericelliott
ericelliott / use-fortmatic.js
Created January 17, 2020 01:30
A React hook for Fortmatic integration.
import { useState, useEffect, useRef } from 'react';
import Web3 from 'web3';
import Fortmatic from 'fortmatic';
const usePromise = () => {
const ref = [];
const container = useRef(ref);
ref[0] = new Promise((resolve, reject) => {
ref[1] = resolve;