-
Notifications
You must be signed in to change notification settings - Fork 7
/
dropWhile.ts
52 lines (45 loc) · 1.62 KB
/
dropWhile.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type {
InferElementType,
InferType,
PH,
Predicate1,
} from './utils/types.ts';
import { slice } from './slice.ts';
import { dispatch } from './utils/dispatch.ts';
import curryN from './utils/curry_n.ts';
import DropWhileTransformer from './utils/Transformers/dropWhile.ts';
// @types
type DropWhile_2<T> = <L extends T[] | string>(list: L) => InferType<L>;
type DropWhile_1<L extends unknown[] | string> = (
predicate: Predicate1<InferElementType<L>>,
) => InferType<L>;
type DropWhile =
& (<T>(predicate: Predicate1<T>) => DropWhile_2<T>)
& (<L extends unknown[] | string>(predicate: PH, list: L) => DropWhile_1<L>)
& (<L extends T[] | string, T>(
predicate: Predicate1<T>,
list: L,
) => InferType<L>);
function _dropWhile<L extends T[] | string, T>(
predicate: Predicate1<T | string>,
functor: L,
) {
const len = functor.length;
let i = 0;
while (i < len && predicate(functor[i])) i++;
return slice(i, Infinity, functor);
}
const dispatchedDropWhile = dispatch(DropWhileTransformer, _dropWhile);
/**
* Returns a new list excluding the leading elements of a `functor` which
* satisfies `predicate`. Skips all the elements which on applied on `predicate`
* returns `true`. The new list starts with first `false`.
*
* Acts as a transducer if a transformer is passed in place of `functor`
*
* const lteTwo = x => x <= 2
* Fae.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
* Fae.dropWhile(x => x !== 't' , 'dispatch'); //=> 'tch'
*/
export const dropWhile: DropWhile = curryN(2, dispatchedDropWhile);