Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
View double-list-declarative.js
const doubleList = list => list.map(x => x * 2);
View double-list-imperative.js
const doubleList = list => {
const newList = [];
for (var i = 0; i < list.length; i++) {
newList[i] = list[i] * 2;
}
return newList;
};
View after-immer.js
import produce from 'immer';
const like = item => ({
type: like.type,
payload: item
});
like.type = 'user/like';
const initialState = {
name: 'Anonymous',
View before-immer.js
const like = item => ({
type: like.type,
payload: item
});
like.type = 'user/like';
const initialState = {
name: 'Anonymous',
avatar: 'Anonymous',
email: '',
View for-await-of-transducers.js
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
function map(f) {
return async function*(values) {
for await (const x of values) {
yield f(x);
}
};
}
@ericelliott
ericelliott / parallel-fetch.js
Created Apr 13, 2020
Parallel requests example
View parallel-fetch.js
// setup
const wait = value => new Promise(resolve => {
setTimeout(() => resolve(value), 3000);
});
const fetchFoo = () => wait('foo');
const fetchBar = () => wait('bar');
const fetchBaz = () => wait('baz');
const fetchDataSlowly = async time => {
View page-hoc.js
import { compose } from 'lodash/fp';
import withFeatures from './with-features';
import withEnv from './with-env';
import withLoader from './with-loader';
import withCoupon from './with-coupon';
import withLayout from './with-layout';
import withAuth from './with-auth';
import { withRouter } from 'next/router';
import withMagicLink from '../features/ethereum-authentication/with-magic-link';
@ericelliott
ericelliott / with-magic-link.js
Created Mar 30, 2020
withMagicLink HOC example.
View with-magic-link.js
View magic-link-tools.js
@ericelliott
ericelliott / use-local-storage.js
Created Mar 30, 2020
A localStorage drop-in replacement for useState
View use-local-storage.js
import { useState } from 'react';
const configureLocalStorage = key => initialValue => {
const [state, setState] = useState(() => {
try {
const value = localStorage.getItem(key);
return value ? JSON.parse(value) : initialValue;
} catch (e) {
console.log(e);
return initialValue;
You can’t perform that action at this time.