Merge pull request #669 from Siarhei-Zharnasek/master

Simplify takeWhile
This commit is contained in:
Angelos Chalaris
2018-05-17 17:27:20 +03:00
committed by GitHub
2 changed files with 3 additions and 3 deletions

View File

@ -2,12 +2,12 @@
Removes elements in an array until the passed function returns `true`. Returns the removed elements. 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`. Loop through the array, using a `for...of` loop over `Array.entries()` until the returned value from the function is `true`.
Return the removed elements, using `Array.slice()`. Return the removed elements, using `Array.slice()`.
```js ```js
const takeWhile = (arr, func) => { const takeWhile = (arr, func) => {
for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
return arr; return arr;
}; };
``` ```

View File

@ -1,5 +1,5 @@
const takeWhile = (arr, func) => { const takeWhile = (arr, func) => {
for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i); for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
return arr; return arr;
}; };
module.exports = takeWhile; module.exports = takeWhile;