diff --git a/README.md b/README.md index 9ab2a84ed..8fb1c87b8 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,8 @@ average(1, 2, 3); * [`tail`](#tail) * [`take`](#take) * [`takeRight`](#takeright) +* [`takeRightWhile`](#takerightwhile) +* [`takeWhile`](#takewhile) * [`union`](#union) * [`unionBy`](#unionby) * [`unionWith`](#unionwith) @@ -2144,6 +2146,59 @@ takeRight([1, 2, 3]); // [3]
[⬆ Back to top](#table-of-contents) +### 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...of` loop over `Array.keys()` until the returned value from the function is `true`. +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); + return arr; +}; +``` + +
+Examples + +```js +takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### takeWhile + +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.keys()` until the returned value from the function is `true`. +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); + return arr; +}; +``` + +
+Examples + +```js +takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### union Returns every element that exists in any of the two arrays once. diff --git a/docs/index.html b/docs/index.html index aded79766..453a9237d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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 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));
+      }

logo 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 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));
 
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.assig
 

takeRight

Returns an array with n elements removed from the end.

Use Array.slice() to create a slice of the array with n elements 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...of loop over Array.keys() until the returned value from the function is true. Return the removed elements, using Array.reverse() and Array.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...of loop over Array.keys() until the returned value from the function is true. Return the removed elements, using Array.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 Set with all values of a and b and 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 Set by applying all fn to all values of a. Create a Set from a and all elements in b whose value, after applying fn does 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;
 };
 ```