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); @@ -123,14 +123,12 @@ Object.assig arrayMax([1, 2, 3]); // 3unary
Creates a function that accepts up to one argument, ignoring any additional arguments.
Call the provided function,
fn, with just the first argument given.const unary = fn => val => fn(val);['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10] -Array
all
Returns
trueif all elements in a collection are truthy,falseotherwise.Use
Array.every(Boolean)to test if all elements in the collection are truthy.const all = arr => arr.every(Boolean); -all([1, 2, 3]); // true -allBy
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.every()to test if all elements in the collection returntruebased onfn.const allBy = (arr, fn) => arr.every(fn); -allBy([4, 2, 3], x => x > 1); // true -any
Returns
trueif at least one element in a collection is truthy,falseotherwise.Use
Array.some(Boolean)to test if any elements in the collection are truthy.const any = arr => arr.some(Boolean); -any([0, 0, 1, 0]); // true -anyBy
Returns
trueif the provided predicate function returnstruefor at least one element in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn.const anyBy = (arr, fn) => arr.some(fn); -anyBy([0, 1, 2, 0], x => x >= 2); // true +Array
all
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.every()to test if all elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const all = (arr, fn = Boolean) => arr.every(fn); +all([4, 2, 3], x => x > 1); // true +all([1, 2, 3]); // true +any
Returns
trueif the provided predicate function returnstruefor at least one element in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const any = (arr, fn = Boolean) => arr.some(fn); +any([0, 1, 2, 0], x => x >= 2); // true +any([0, 0, 1, 0]); // truebifurcate
Splits values into two groups. If an element in
filteris truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.Use
Array.reduce()andArray.push()to add elements to groups, based onfilter.const bifurcate = (arr, filter) => arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] @@ -297,10 +295,9 @@ Object.assigminN
Returns the
nminimum elements from the provided array. Ifnis greater than or equal to the provided array's length, then return the original array(sorted in ascending order).Use
Array.sort()combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. UseArray.slice()to get the specified number of elements. Omit the second argument,n, to get a one-element array.const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);minN([1, 2, 3]); // [1] minN([1, 2, 3], 2); // [1,2] -none
Returns
trueif no elements in a collection are truthy,falseotherwise.Use
!Array.some(Boolean)to test if any elements in the collection are truthy.const none = arr => !arr.some(Boolean); -none([0, 0, 0]); // true -noneBy
Returns
trueif the provided predicate function returnsfalsefor all elements in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn.const noneBy = (arr, fn) => !arr.some(fn); -noneBy([0, 1, 3, 0], x => x == 2); // true +none
Returns
trueif the provided predicate function returnsfalsefor all elements in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const none = (arr, fn = Boolean) => !arr.some(fn); +none([0, 1, 3, 0], x => x == 2); // true +none([0, 0, 0]); // truenthElement
Returns the nth element of an array.
Use
Array.slice()to get an array containing the nth element at the first place. If the index is out of bounds, return[]. Omit the second argument,n, to get the first element of the array.const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];nthElement(['a', 'b', 'c'], 1); // 'b' nthElement(['a', 'b', 'b'], -3); // 'a'