This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const defaultState = 0; | |
const reducer = (state = defaultState, action) => { | |
switch (action.type) { | |
case 'ADD': return state + action.payload; | |
default: return state; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actions = [ | |
{ type: 'ADD', payload: 0 }, | |
{ type: 'ADD', payload: 1 }, | |
{ type: 'ADD', payload: 2 } | |
]; | |
const total = actions.reduce(reducer, 0); // 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |