Skip to content

Instantly share code, notes, and snippets.

Jamie Dixon JamieDixon

Block or report user

Report or block JamieDixon

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View paren-match-recur.js
const Empty = Symbol();
const stateIdentity = 0;
const adjusters = {
'(': 1,
')': -1
}
const getClosingParenPositionRec = (start, str) => {
View list-applicative.md
View useAutosave.js
const useAutosave = (saveFn, timeout, dependencies) => {
const isFirstLoad = useRef(true);
useEffect(() => {
if (isFirstLoad.current) {
isFirstLoad.current = false;
return;
}
const timer = setTimeout(saveFn, 1000);
View sort-array-by-list.js
const sortArrayByList = (fn, arr, order) =>
arr.sort(
(item1, item2) =>
order.indexOf(fn(item1)) > order.indexOf(fn(item2)) ? 1 : -1
);
const selector = x => x.name;
const arr = [{ name: 'Jamie' }, { name: 'Simon' }, { name: 'Paul' }, { name: 'Stuart' }];
const order = ['Stuart', 'Paul', 'Simon', 'Jamie'];
View useThrottle.js
const useThrottle = (fn, wait) => {
const throttledRef = useRef(fn);
const lastCallRef = useRef({ fn: () => {}, args: null });
useEffect(
() => {
let isThrottling = false;
throttledRef.current = (...args) => {
if (isThrottling) {
lastCallRef.current = {
View theme-switcher-react17.js
const themes = {
localhost: () => import('../../styles/themes/default.js'),
'client1.local.com': import('../../styles/themes/client1.js'),
'client2.local.com': import('../../styles/themes/client2.js')
};
const Select = styled.select`
position: absolute;
bottom: 1em;
left: 1em;
View theme-switcher-react16.js
const themes = {
localhost: () => import('../../styles/themes/default.js'),
'client1.local.com': import('../../styles/themes/client1.js'),
'client2.local.com': import('../../styles/themes/client2.js')
};
const Select = styled.select`
position: absolute;
bottom: 1em;
left: 1em;
View makeGeneratorSnapshot.js
export const makeGeneratorSnapshot = (gen, params = {}) => {
if (!gen) {
throw new Error('You must pass a generator function as the first parameter to makeGeratorSnapshot');
}
const iter = gen(params[0]);
let i = 1;
while (true) {
// eslint-disable-line no-constant-condition
const param = params[i];
View curry.js
// A contrived compose function.
const compose = (f1, f2) => (...args) => f1(f2(...args));
// These functions are curried. Instead of the usual (a, b) => a + b, we're supplying each parameter individually
// and then returning a function that takes the next param.
// This is currying. We're taking a function that normally has 2 params, and turning it into a function that takes 1 param,
// then returns another function expecting the second param.
const add = a => b => a + b;
const multiply = a => b => a * b;
View compose2.js
const compose = (...fns) => {
function _compose(funcs, next, ...args) {
const n = next(...args);
return funcs.length ? _compose(funcs, funcs.pop(), n) : n;
}
return (...args) => {
const funcs = [...fns];
return _compose(funcs, funcs.pop(), ...args)
You can’t perform that action at this time.