Skip to content

Commit e562fce

Browse files
committed
feat(filter): implement filter
1 parent 91ef489 commit e562fce

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const nativeConcat = Array.prototype.concat;
1010
// tslint:disable-next-line:no-unbound-method
1111
const nativeMap = Array.prototype.map;
1212

13+
// tslint:disable-next-line:no-unbound-method
14+
const nativeFilter = Array.prototype.filter;
15+
1316
export const copy: <T>(array: ArrayLike<T>) => T[] =
1417
Array.from != null
1518
? Array.from // tslint:disable-line:no-unbound-method
@@ -95,6 +98,20 @@ export function mapFn<T, U>(f: (element: T, index: number) => U): (array: ArrayL
9598
return array => map(array, f);
9699
}
97100

101+
export function filter<T, U extends T>(array: ArrayLike<T>,
102+
predicate: (element: T, index: number) => element is U): U[];
103+
export function filter<T>(array: ArrayLike<T>, predicate: (element: T, index: number) => boolean): T[];
104+
export function filter<T>(array: ArrayLike<T>, predicate: (element: T, index: number) => boolean): T[] {
105+
return nativeFilter.call(array, predicate);
106+
}
107+
108+
export function filterFn<T, U extends T>(predicate: (element: T, index: number) => element is U)
109+
: (array: ArrayLike<T>) => U[];
110+
export function filterFn<T>(predicate: (element: T, index: number) => boolean): (array: ArrayLike<T>) => T[];
111+
export function filterFn<T>(predicate: (element: T, index: number) => boolean): (array: ArrayLike<T>) => T[] {
112+
return array => filter(array, predicate);
113+
}
114+
98115
export function keyBy<T>(array: ArrayLike<T>,
99116
f: (element: T) => string): Dictionary<T[]> {
100117
const dictionary = {} as Dictionary<T[]>;

0 commit comments

Comments
 (0)