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,11 +125,11 @@ 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] -dropElements
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 dropElements = (arr, func) => { +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; }; -dropElements([1, 2, 3, 4], n => n >= 3); // [3,4] +dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4]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] diff --git a/snippets/drop.md b/snippets/drop.md new file mode 100644 index 000000000..ce5c93374 --- /dev/null +++ b/snippets/drop.md @@ -0,0 +1,15 @@ +### drop + +Returns a new array with `n` elements removed from the left. + +Use `Array.slice()` to slice the remove the specified number of elements from the left. + +```js +const drop = (arr, n = 1) => arr.slice(n); +``` + +```js +drop([1, 2, 3]); // [2,3] +drop([1, 2, 3], 2); // [3] +drop([1, 2, 3], 42); // [] +``` diff --git a/snippets/dropRightWhile.md b/snippets/dropRightWhile.md new file mode 100644 index 000000000..2329e65c0 --- /dev/null +++ b/snippets/dropRightWhile.md @@ -0,0 +1,17 @@ +### 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 is `true`. +Returns the remaining elements. + +```js +const dropRightWhile = (arr, func) => { + while (arr.length > 0 && !func(arr[arr.length-1])) arr = arr.slice(0, -1); + return arr; +}; +``` + +```js +dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2] +``` diff --git a/snippets/dropElements.md b/snippets/dropWhile.md similarity index 78% rename from snippets/dropElements.md rename to snippets/dropWhile.md index bb99b9099..44c4d6cee 100644 --- a/snippets/dropElements.md +++ b/snippets/dropWhile.md @@ -1,4 +1,4 @@ -### dropElements +### dropWhile Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array. @@ -6,12 +6,12 @@ Loop through the array, using `Array.slice()` to drop the first element of the a Returns the remaining elements. ```js -const dropElements = (arr, func) => { +const dropWhile = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; ``` ```js -dropElements([1, 2, 3, 4], n => n >= 3); // [3,4] +dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4] ``` diff --git a/tag_database b/tag_database index 80db02651..e7a0921bd 100644 --- a/tag_database +++ b/tag_database @@ -43,8 +43,10 @@ differenceBy:array,function differenceWith:array,function digitize:math,array distance:math -dropElements:array,function +drop:array dropRight:array +dropRightWhile:array,function +dropWhile:array,function elementIsVisibleInViewport:browser elo:math,array,advanced equals:object,array,type,advanced diff --git a/test/dropElements/dropElements.js b/test/dropElements/dropElements.js index bb87783f9..655598419 100644 --- a/test/dropElements/dropElements.js +++ b/test/dropElements/dropElements.js @@ -1,5 +1,5 @@ -const dropElements = (arr, func) => { +const dropWhile = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; -module.exports = dropElements \ No newline at end of file +module.exports = dropWhile \ No newline at end of file diff --git a/test/dropElements/dropElements.test.js b/test/dropElements/dropElements.test.js index b92ecd37d..bfd4e4b0b 100644 --- a/test/dropElements/dropElements.test.js +++ b/test/dropElements/dropElements.test.js @@ -1,14 +1,14 @@ const test = require('tape'); -const dropElements = require('./dropElements.js'); +const dropWhile = require('./dropWhile.js'); -test('Testing dropElements', (t) => { +test('Testing dropWhile', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape - t.true(typeof dropElements === 'function', 'dropElements is a Function'); - t.deepEqual(dropElements([1, 2, 3, 4], n => n >= 3), [3,4], "Removes elements in an array until the passed function returns true"); - //t.deepEqual(dropElements(args..), 'Expected'); - //t.equal(dropElements(args..), 'Expected'); - //t.false(dropElements(args..), 'Expected'); - //t.throws(dropElements(args..), 'Expected'); + t.true(typeof dropWhile === 'function', 'dropWhile is a Function'); + t.deepEqual(dropWhile([1, 2, 3, 4], n => n >= 3), [3,4], "Removes elements in an array until the passed function returns true"); + //t.deepEqual(dropWhile(args..), 'Expected'); + //t.equal(dropWhile(args..), 'Expected'); + //t.false(dropWhile(args..), 'Expected'); + //t.throws(dropWhile(args..), 'Expected'); t.end(); }); \ No newline at end of file diff --git a/test/testlog b/test/testlog index 081f066d0..5231e9992 100644 --- a/test/testlog +++ b/test/testlog @@ -252,9 +252,9 @@ Test log for: Fri Jan 26 2018 12:00:18 GMT+0200 (GTB Standard Time) √ distance is a Function - Testing dropElements + Testing dropWhile - √ dropElements is a Function + √ dropWhile is a Function √ Removes elements in an array until the passed function returns true Testing dropRight