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); @@ -406,6 +406,17 @@ Object.assigtakeRight
Returns an array with n elements removed from the end.
Use
Array.slice()to create a slice of the array withnelements taken from the end.const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);takeRight([1, 2, 3], 2); // [ 2, 3 ] takeRight([1, 2, 3]); // [3] +takeRightWhile
Removes elements from the end of an array until the passed function returns
true. Returns the removed elements.Loop through the array, using a
for...ofloop overArray.keys()until the returned value from the function istrue. Return the removed elements, usingArray.reverse()andArray.slice().const takeRightWhile = (arr, func) => { + for (let i of arr.reverse().keys()) + if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length); + return arr; +}; +takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4] +takeWhile
Removes elements in an array until the passed function returns
true. Returns the removed elements.Loop through the array, using a
for...ofloop overArray.keys()until the returned value from the function istrue. Return the removed elements, usingArray.slice().const takeWhile = (arr, func) => { + for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); + return arr; +}; +takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]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]unionBy
Returns every element that exists in any of the two arrays once, after applying the provided function to each array element of both.
Create a
Setby applying allfnto all values ofa. Create aSetfromaand all elements inbwhose value, after applyingfndoes not match a value in the previously created set. Return the last set converted to an array.const unionBy = (a, b, fn) => { diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md index 7a1d9c57b..526124ac8 100644 --- a/snippets/takeRightWhile.md +++ b/snippets/takeRightWhile.md @@ -8,7 +8,7 @@ Return the removed elements, using `Array.reverse()` and `Array.slice()`. ```js const takeRightWhile = (arr, func) => { for (let i of arr.reverse().keys()) - if (func(arr[i])) return arr.reverse().slice(arr.length - i,arr.length); + if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length); return arr; }; ``` diff --git a/snippets/takeWhile.md b/snippets/takeWhile.md index 38c32b81d..374606427 100644 --- a/snippets/takeWhile.md +++ b/snippets/takeWhile.md @@ -7,8 +7,7 @@ Return the removed elements, using `Array.slice()`. ```js const takeWhile = (arr, func) => { - for(let i of arr.keys()) - if (func(arr[i])) return arr.slice(0,i); + for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); return arr; }; ```