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); @@ -367,15 +367,32 @@ Object.assig };sortedIndex([5, 3, 2, 1], 4); // 1 sortedIndex([30, 50], 40); // 1 -sortedLastIndex
Returns the highest index at which value should be inserted into array in order to maintain its sort order.
Check if the array is sorted in descending order (loosely). Use
Array.map()to map each element to an array with its index and value. UseArray.filter()to find all possible positions where the element could be inserted,Array.slice(-1)to get the last one.const sortedLastIndex = (arr, n) => { +sortedIndexBy
Returns the lowest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.
Check if the array is sorted in descending order (loosely). Use
Array.findIndex()to find the appropriate index where the element should be inserted, based on the iterator functionfn.const sortedIndexBy = (arr, n, fn) => { + const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]); + const val = fn(n); + const index = arr.findIndex(el => (isDescending ? val >= fn(el) : val <= fn(el))); + return index === -1 ? arr.length : index; +}; +sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0 +sortedLastIndex
Returns the highest index at which value should be inserted into array in order to maintain its sort order.
Check if the array is sorted in descending order (loosely). Use
Array.map()to map each element to an array with its index and value. UseArray.reverse()andArray.findIndex()to find the appropriate last index where the element should be inserted.const sortedLastIndex = (arr, n) => { const isDescending = arr[0] > arr[arr.length - 1]; const index = arr .map((val, i) => [i, val]) - .filter(el => (isDescending ? n >= el[1] : n >= el[1])) - .slice(-1)[0][0]; - return index === -1 ? arr.length : index; + .reverse() + .findIndex(el => (isDescending ? n <= el[1] : n >= el[1])); + return index === -1 ? 0 : arr.length - index - 1; };sortedLastIndex([10, 20, 30, 30, 40], 30); // 3 +sortedLastIndexBy
Returns the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.
Check if the array is sorted in descending order (loosely). Use
Array.reverse()andArray.findIndex()to find the appropriate last index where the element should be inserted, based on the iterator functionfn..const sortedLastIndexBy = (arr, n, fn) => { + const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]); + const val = fn(n); + const index = arr + .map((val, i) => [i, fn(val)]) + .reverse() + .findIndex(el => (isDescending ? val <= el[1] : val >= el[1])); + return index === -1 ? 0 : arr.length - index; +}; +sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1symmetricDifference
Returns the symmetric difference between two arrays.
Create a
Setfrom each array, then useArray.filter()on each of them to only keep values not contained in the other.const symmetricDifference = (a, b) => { const sA = new Set(a), sB = new Set(b); diff --git a/snippets/sortedIndexBy.md b/snippets/sortedIndexBy.md index 97335dc58..b57a26947 100644 --- a/snippets/sortedIndexBy.md +++ b/snippets/sortedIndexBy.md @@ -15,5 +15,5 @@ const sortedIndexBy = (arr, n, fn) => { ``` ```js -sortedIndexBy([{ 'x': 4 }, { 'x': 5 }], { 'x': 4 }, o => o.x); // 0 +sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0 ``` diff --git a/snippets/sortedLastIndexBy.md b/snippets/sortedLastIndexBy.md index 02ad7ce60..443ba5045 100644 --- a/snippets/sortedLastIndexBy.md +++ b/snippets/sortedLastIndexBy.md @@ -18,5 +18,5 @@ const sortedLastIndexBy = (arr, n, fn) => { ``` ```js -sortedLastIndexBy([{ 'x': 4 }, { 'x': 5 }], { 'x': 4 }, o => o.x); // 1 +sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1 ```