forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropWhile.ts
More file actions
27 lines (24 loc) · 797 Bytes
/
dropWhile.ts
File metadata and controls
27 lines (24 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { dropWhile } from '../typings/types';
import curryN from '../function/curryN';
/**
* Returns a new list excluding the leading elements of a given list which
* satisfy the supplied predicate function. It passes each value to the supplied
* predicate function, skipping elements while the predicate function returns
* `true`.
*
* @param {Function} fn The function called per iteration.
* @param {Array} arr The collection to iterate over.
* @return {Array} A new array.
* @example
*
* var lteTwo = x => x <= 2;
*
* dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
*/
export default curryN(2, (fn, arr = []) => {
let idx = 0;
while (idx < arr.length && fn(arr[idx])) {
idx += 1;
}
return arr.slice(idx);
}) as typeof dropWhile