Note
Highlights information that users should take into account, even when skimming.
Tip
Optional information to help a user be more successful.
Important
Crucial information necessary for users to succeed.
[!WARNING]
Note
Highlights information that users should take into account, even when skimming.
Tip
Optional information to help a user be more successful.
Important
Crucial information necessary for users to succeed.
[!WARNING]
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); |
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}), {}); | |
})(); |
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; | |
}; |
const zipObject = (keys, vals) => { | |
return keys.reduce( | |
(prev, val, i) => Object.assign(prev, { [val]: vals[i] }), {} | |
) | |
} |
const toTitle = str => { | |
return str.split(/[\W_]+/) | |
.map(word => word.replace(/^\w/, first => first.toUpperCase())) | |
.join(' ') | |
} |
export function callDeep (path, obj, ...args) { | |
var fn = path.split('.').reduce((prev, curr) => prev[curr], obj) | |
return fn.apply(obj, args) | |
} |