This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-array.ts
45 lines (42 loc) · 1.76 KB
/
filter-array.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
import { makePushIterable } from '../base';
import { arrayLike$elementOf } from '../base/array-like.impl';
import type { PushIterable } from '../push-iterable';
import { filterIndexed$ } from './filter-indexed.impl';
/**
* Creates a {@link PushIterable push iterable} with all `array` elements extending the given type.
*
* @typeParam T - A type of array elements.
* @typeParam TTarget - Target type.
* @param array - A source array.
* @param test - A predicate function to test that element extends the type `TTarget`. Returns `true` to keep the
* element, or `false` otherwise. It accepts the tested element as the only parameter.
*
* @returns New push iterable with the elements that pass the test. If no elements passed the test, an empty iterable
* will be returned.
*/
export function filterArray<T, TTarget extends T>(
array: ArrayLike<T>,
test: (this: void, element: T) => element is TTarget,
): PushIterable<TTarget>;
/**
* Creates a {@link PushIterable | push iterable} with all `array` elements that pass the test implemented by
* the provided function.
*
* @typeParam T - A type of array elements.
* @param array - A source array.
* @param test - A predicate function to test each element. Returns `true` to keep the element, or `false` otherwise.
* It accepts the tested element as the only parameter.
*
* @returns New push iterable with the elements that pass the test. If no elements passed the test, an empty iterable
* will be returned.
*/
export function filterArray<T>(
array: ArrayLike<T>,
test: (this: void, element: T) => boolean,
): PushIterable<T>;
export function filterArray<T>(
array: ArrayLike<T>,
test: (this: void, element: T) => boolean,
): PushIterable<T> {
return makePushIterable(filterIndexed$(array, arrayLike$elementOf, test));
}