Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.slice(0,n)and the spread operator (...).const ary = (fn, n) => (...args) => fn(...args.slice(0, n));const firstTwoMax = ary(Math.max, 2); [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]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); @@ -311,6 +311,14 @@ Object.assigreduceSuccessive
Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.
Use
Array.reduce()to apply the given function to the given array, storing each new result.const reduceSuccessive = (arr, fn, acc) => arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21] +reduceWhich
Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.
Use
Array.reduce()in combination with thecomparatorfunction to get the appropriate element in the array. You can omit the second parameter,comparator, to use the default one that returns the minimum element in the array.const reduceWhich = (arr, comparator = (a, b) => a - b) => + arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); +reduceWhich([1, 3, 2]); // 1 +reduceWhich([1, 3, 2], (a, b) => b - a); // 3 +reduceWhich( + [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], + (a, b) => a.age - b.age +); // {name: "Lucy", age: 9}remove
Removes elements from an array for which the given function returns
false.Use
Array.filter()to find array elements that return truthy values andArray.reduce()to remove elements usingArray.splice(). Thefuncis invoked with three arguments (value, index, array).const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { @@ -1706,12 +1714,4 @@ Logs: { yesNo('yes'); // true yesNo('No'); // false yesNo('Foo', true); // true -Uncategorized
reduceWhich
Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.
Use
Array.reduce()in combination with thecomparatorfunction to get the appropriate element in the array. You can omit the second parameter,comparator, to use the default one that returns the minimum element in the array.const reduceWhich = (arr, comparator = (a, b) => a - b) => - arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); -reduceWhich([1, 3, 2]); // 1 -reduceWhich([1, 3, 2], (a, b) => b - a); // 3 -reduceWhich( - [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], - (a, b) => a.age - b.age -); // {name: "Lucy", age: 9} -