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 ] @@ -231,9 +231,6 @@ Object.assig );const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }]; partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]] -pick
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 pair if the key exists in the obj.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 }pull
Mutates the original array to filter out the values specified.
Use
Array.filter()andArray.includes()to pull out the values that are not needed. UseArray.length = 0to mutate the passed in an array by resetting it's length to zero andArray.push()to re-populate it with only the pulled values.(For a snippet that does not mutate the original array see
without)const pull = (arr, ...args) => { let argState = Array.isArray(args[0]) ? args[0] : args; let pulled = arr.filter((v, i) => !argState.includes(v)); @@ -1008,6 +1005,11 @@ Foo.prototypeShow examplesobjectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}objectToPairs
Creates an array of key-value pair arrays from an object.
Use
Object.keys()andArray.map()to iterate over the object's keys and produce an array with key-value pairs.const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]] +omit
Omits the key-value pairs corresponding to the given keys from an object.
Use
Object.keys(obj),Array.filter()andArray.includes()to remove the provided keys. UseArray.reduce()to convert the filtered keys back to an object with the corresponding key-value pairs.const omit = (obj, arr) => + Object.keys(obj) + .filter(k => !arr.includes(k)) + .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); +omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }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) => { @@ -1021,6 +1023,9 @@ Foo.prototypeShow examplesconst users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }]; orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}] orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}] +pick
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 }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