Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

export const memoizedAdd = () => {
const cache = new Map();
return (...args) => {
const key = args.join();
const cached = cache.get(key);
if (cached) {
console.log(`Returning cached value for ${key}`);
return cached;
} else {
const result = args.reduce((total, val) => total + val);
@erikjung
erikjung / index.js
Created April 18, 2018 01:50
Merging results of Promise.all
const a = new Promise(r => r({ a: 'A' }));
const b = new Promise(r => r({ b: 'B' }));
const c = new Promise(r => r({ c: 'C' }));
const result = (async () => {
const abc = await Promise.all([a, b, c]);
return abc.reduce((result, obj) => ({...result, ...obj}), {});
})();
@erikjung
erikjung / fp-array-utils.js
Created March 2, 2018 10:52
A snippet to generate FP array one-liners
const ArrayUtils = [
'map',
'filter',
'find',
]
.reduce((utils, method) => ({
...utils,
[method]: (fn, arr) => arr ? arr[method](fn) : arr => arr[method](fn)
}), {})
export default const renameProps = (map, obj) =>
Object.entries(map).reduce((accum, [a, b]) => {
delete accum[a]
return obj[a]
? Object.assign(accum, {[b]: obj[a]})
: accum
}, {...obj})
/**
* Tweens a number from `startValue` to `endValue` over the `duration` time period,
* calling `onAnimationFrame` on each animation frame.
*
* @example
* // Tween to 100 from 0 over 2 seconds
* tweenNumber(n => console.log(`step value: ${n}`), 100, 0, 2000);
*/
function tweenNumber (
onAnimationFrame,
module.exports = function leftpad (str, len, ch) {
const val = String(str);
const pad = ch === undefined ? ' ' : String(ch);
return pad.repeat(Math.max(0, len - val.length), pad) + val;
};
@erikjung
erikjung / zipObject.js
Last active February 27, 2016 20:17
This combines two arrays into an object.
const zipObject = (keys, vals) => {
return keys.reduce(
(prev, val, i) => Object.assign(prev, { [val]: vals[i] }), {}
)
}
@erikjung
erikjung / toTitle.js
Last active February 22, 2016 19:18
toTitle
const toTitle = str => {
return str.split(/[\W_]+/)
.map(word => word.replace(/^\w/, first => first.toUpperCase()))
.join(' ')
}
@erikjung
erikjung / index.js
Last active January 26, 2016 01:01
callDeep
export function callDeep (path, obj, ...args) {
var fn = path.split('.').reduce((prev, curr) => prev[curr], obj)
return fn.apply(obj, args)
}
@erikjung
erikjung / index.css
Last active January 10, 2016 22:57
Base styles
:root {
--scale-ratio: 1.4;
--scale-bases: 1 1.2;
}