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 ] @@ -111,8 +111,6 @@ Object.assigdifference([1, 2, 3], [1, 2, 4]); // [3]differenceWith
Filters out all values from an array for which the comparator function does not return
true.Use
Array.filter()andArray.findIndex()to find the appropriate values.const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2] -distinctValuesOfArray
Returns all the distinct values of an array.
Use ES6
Setand the...restoperator to discard all duplicated values.const distinctValuesOfArray = arr => [...new Set(arr)]; -distinctValuesOfArray([1, 2, 2, 3, 4, 4, 5]); // [1,2,3,4,5]dropElements
Removes elements in an array until the passed function returns
true. Returns the remaining elements in the array.Loop through the array, using
Array.slice()to drop the first element of the array until the returned value from the function istrue. Returns the remaining elements.const dropElements = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; @@ -342,6 +340,8 @@ Object.assig takeRight([1, 2, 3]); // [3]union
Returns every element that exists in any of the two arrays once.
Create a
Setwith all values ofaandband convert to an array.const union = (a, b) => Array.from(new Set([...a, ...b]));union([1, 2, 3], [4, 3, 2]); // [1,2,3,4] +uniqueElements
Returns all unique values of an array.
Use ES6
Setand the...restoperator to discard all duplicated values.const uniqueElements = arr => [...new Set(arr)]; +uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1,2,3,4,5]without
Filters out the elements of an array, that have one of the specified values.
Use
Array.filter()to create an array excluding(using!Array.includes()) all given values.(For a snippet that mutates the original array see
pull)const without = (arr, ...args) => arr.filter(v => !args.includes(v));without([2, 1, 2, 3], 1, 2); // [3]zip
Creates an array of elements, grouped based on the position in the original arrays.
Use
Math.max.apply()to get the longest array in the arguments. Creates an array with that length as return value and useArray.from()with a map-function to create an array of grouped elements. If lengths of the argument-arrays vary,undefinedis used where no value could be found.const zip = (...arrays) => {