-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.ts
76 lines (71 loc) · 2.79 KB
/
filter.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { LazyEvaluator } from './pipe';
import { curry } from './curry';
import { SKIP_ITEM } from './internal/utility-evaluators';
/**
* Creates a shallow copy of a portion of a given array, filtered down to just
* the elements from the given array that pass the test implemented by the
* provided function. Equivalent to `Array.prototype.filter`.
*
* @param data - The array to filter.
* @param predicate - A function to execute for each element in the array. It
* should return `true` to keep the element in the resulting array, and `false`
* otherwise. A type-predicate can also be used to narrow the result.
* @returns A shallow copy of the given array containing just the elements that
* pass the test. If no elements pass the test, an empty array is returned.
* @signature
* P.filter(data, predicate)
* @example
* P.filter([1, 2, 3], x => x % 2 === 1) // => [1, 3]
* @dataFirst
* @lazy
* @category Array
*/
export function filter<T, S extends T>(
data: ReadonlyArray<T>,
predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,
): Array<S>;
export function filter<T>(
data: ReadonlyArray<T>,
predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,
): Array<T>;
/**
* Creates a shallow copy of a portion of a given array, filtered down to just
* the elements from the given array that pass the test implemented by the
* provided function. Equivalent to `Array.prototype.filter`.
*
* @param predicate - A function to execute for each element in the array. It
* should return `true` to keep the element in the resulting array, and `false`
* otherwise.
* @returns A shallow copy of the given array containing just the elements that
* pass the test. If no elements pass the test, an empty array is returned.
* @signature
* P.filter(predicate)(data)
* @example
* P.pipe([1, 2, 3], P.filter(x => x % 2 === 1)) // => [1, 3]
* @dataLast
* @lazy
* @category Array
*/
export function filter<T, S extends T>(
predicate: (value: T, index: number, data: ReadonlyArray<T>) => value is S,
): (data: ReadonlyArray<T>) => Array<S>;
export function filter<T>(
predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,
): (data: ReadonlyArray<T>) => Array<T>;
export function filter(...args: ReadonlyArray<unknown>): unknown {
return curry(filterImplementation, args, lazyImplementation);
}
function filterImplementation<T>(
data: ReadonlyArray<T>,
predicate: (value: T, index: number, array: ReadonlyArray<T>) => boolean,
): Array<T> {
return data.filter(predicate);
};
function lazyImplementation<T>(
predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,
): LazyEvaluator<T> {
return (value, index, data) =>
predicate(value, index, data)
? { done: false, hasNext: true, next: value }
: SKIP_ITEM;
}