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); @@ -125,15 +125,24 @@ Object.assig differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ]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] -dropWhile
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 dropWhile = (arr, func) => { - while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); - return arr; -}; -dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4] +drop
Returns a new array with
nelements removed from the left.Use
Array.slice()to slice the remove the specified number of elements from the left.const drop = (arr, n = 1) => arr.slice(n); +drop([1, 2, 3]); // [2,3] +drop([1, 2, 3], 2); // [3] +drop([1, 2, 3], 42); // []dropRight
Returns a new array with
nelements removed from the right.Use
Array.slice()to slice the remove the specified number of elements from the right.const dropRight = (arr, n = 1) => arr.slice(0, -n);dropRight([1, 2, 3]); // [1,2] dropRight([1, 2, 3], 2); // [1] dropRight([1, 2, 3], 42); // [] +dropRightWhile
Removes elements from the end of 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 last element of the array until the returned value from the function istrue. Returns the remaining elements.const dropRightWhile = (arr, func) => { + while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1); + return arr; +}; +dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2] +dropWhile
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 dropWhile = (arr, func) => { + while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); + return arr; +}; +dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4]everyNth
Returns every nth element in an array.
Use
Array.filter()to create a new array that contains every nth element of a given array.const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]filterNonUnique
Filters out the non-unique values in an array.
Use
Array.filter()for an array containing only the unique values.const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i)); diff --git a/snippets/dropRightWhile.md b/snippets/dropRightWhile.md index 2329e65c0..4077afd24 100644 --- a/snippets/dropRightWhile.md +++ b/snippets/dropRightWhile.md @@ -7,7 +7,7 @@ Returns the remaining elements. ```js const dropRightWhile = (arr, func) => { - while (arr.length > 0 && !func(arr[arr.length-1])) arr = arr.slice(0, -1); + while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1); return arr; }; ```