Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -1010,6 +1010,11 @@ Foo.prototypefilter(k => !arr.includes(k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 } +omitBy
Creates an object composed of the properties the given function returns falsey for. The function is invoked with two arguments: (value, key).
Use
Object.keys(obj)andArray.filter()to remove the keys for whichfnreturns a truthy value. UseArray.reduce()to convert the filtered keys back to an object with the corresponding key-value pairs.const omitBy = (obj, fn) => + Object.keys(obj) + .filter(k => !fn(obj[k], k)) + .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); +omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { b: '2' }orderBy
Returns a sorted array of objects ordered by properties and orders.
Uses
Array.sort(),Array.reduce()on thepropsarray with a default value of0, use array destructuring to swap the properties position depending on the order passed. If noordersarray is passed it sort by'asc'by default.const orderBy = (arr, props, orders) => [...arr].sort((a, b) => props.reduce((acc, prop, i) => { @@ -1026,6 +1031,11 @@ Foo.prototype📋 Copy to clipboardpick
Picks the key-value pairs corresponding to the given keys from an object.
Use
Array.reduce()to convert the filtered/picked keys back to an object with the corresponding key-value pairs if the key exists in the object.const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 } +pickBy
Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).
Use
Object.keys(obj)andArray.filter()to remove the keys for whichfnreturns a falsey value. UseArray.reduce()to convert the filtered keys back to an object with the corresponding key-value pairs.const pickBy = (obj, fn) => + Object.keys(obj) + .filter(k => fn(obj[k], k)) + .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); +pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { 'a': 1, 'c': 3 }shallowClone
Creates a shallow clone of an object.
Use
Object.assign()and an empty object ({}) to create a shallow clone of the original.const shallowClone = obj => Object.assign({}, obj);const a = { x: true, y: 1 }; const b = shallowClone(a); // a !== b