From 959c114b0e04b3766fd0f5a9bd6ab6229ef0b141 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 29 Nov 2020 12:04:53 +0200 Subject: [PATCH] Update takeWhile snippets Add takeUntil snippets --- snippets/takeRightUntil.md | 24 ++++++++++++++++++++++++ snippets/takeRightWhile.md | 16 +++++++++++----- snippets/takeUntil.md | 22 ++++++++++++++++++++++ snippets/takeWhile.md | 7 ++++--- 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 snippets/takeRightUntil.md create mode 100644 snippets/takeUntil.md diff --git a/snippets/takeRightUntil.md b/snippets/takeRightUntil.md new file mode 100644 index 000000000..fd27a35a4 --- /dev/null +++ b/snippets/takeRightUntil.md @@ -0,0 +1,24 @@ +--- +title: takeRightUntil +tags: array,intermediate +--- + +Removes elements from the end of an array until the passed function returns `true`. +Returns the removed elements. + +- Create a reversed copy of the array, using the spread operator (`...`) and `Array.prototype.reverse()`. +- Loop through the reversed copy, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is truthy. +- Return the removed elements, using `Array.prototype.slice()`. +- The callback function, `fn`, accepts a single argument which is the value of the element. + +```js +const takeRightUntil = (arr, fn) => { + for (const [i, val] of [...arr].reverse().entries()) + if (fn(val)) return i === 0 ? [] : arr.slice(-i); + return arr; +}; +``` + +```js +takeRightUntil([1, 2, 3, 4], n => n < 3); // [3, 4] +``` diff --git a/snippets/takeRightWhile.md b/snippets/takeRightWhile.md index 66e4646b7..03ff8c342 100644 --- a/snippets/takeRightWhile.md +++ b/snippets/takeRightWhile.md @@ -3,16 +3,22 @@ title: takeRightWhile tags: array,intermediate --- -Removes elements from the end of an array until the passed function returns `true`. +Removes elements from the end of an array until the passed function returns `false`. Returns the removed elements. -- Loop through the array, using `Array.prototype.reduceRight()` and accumulating elements while `func` returns falsy values. +- Create a reversed copy of the array, using the spread operator (`...`) and `Array.prototype.reverse()`. +- Loop through the reversed copy, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is falsy. +- Return the removed elements, using `Array.prototype.slice()`. +- The callback function, `fn`, accepts a single argument which is the value of the element. ```js -const takeRightWhile = (arr, func) => - arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []); +const takeRightWhile = (arr, fn) => { + for (const [i, val] of [...arr].reverse().entries()) + if (!fn(val)) return i === 0 ? [] : arr.slice(-i); + return arr; +}; ``` ```js -takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4] +takeRightWhile([1, 2, 3, 4], n => n >= 3); // [3, 4] ``` diff --git a/snippets/takeUntil.md b/snippets/takeUntil.md new file mode 100644 index 000000000..093eda2f1 --- /dev/null +++ b/snippets/takeUntil.md @@ -0,0 +1,22 @@ +--- +title: takeUntil +tags: array,intermediate +--- + +Removes elements in an array until the passed function returns `true`. +Returns the removed elements. + +- Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is truthy. +- Return the removed elements, using `Array.prototype.slice()`. +- The callback function, `fn`, accepts a single argument which is the value of the element. + +```js +const takeUntil = (arr, fn) => { + for (const [i, val] of arr.entries()) if (fn(val)) return arr.slice(0, i); + return arr; +}; +``` + +```js +takeUntil([1, 2, 3, 4], n => n >= 3); // [1, 2] +``` diff --git a/snippets/takeWhile.md b/snippets/takeWhile.md index 2177e2071..8aea24ae7 100644 --- a/snippets/takeWhile.md +++ b/snippets/takeWhile.md @@ -6,12 +6,13 @@ tags: array,intermediate Removes elements in an array until the passed function returns `false`. Returns the removed elements. -- Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is `false`. +- Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is falsy. - Return the removed elements, using `Array.prototype.slice()`. +- The callback function, `fn`, accepts a single argument which is the value of the element. ```js -const takeWhile = (arr, func) => { - for (const [i, val] of arr.entries()) if (!func(val)) return arr.slice(0, i); +const takeWhile = (arr, fn) => { + for (const [i, val] of arr.entries()) if (!fn(val)) return arr.slice(0, i); return arr; }; ```