forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.ts
More file actions
28 lines (25 loc) · 671 Bytes
/
filter.ts
File metadata and controls
28 lines (25 loc) · 671 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
28
import curryN from '../function/curryN';
import { filter } from '../typings/types';
/**
* Takes a predicate and a "arr", and returns a new array of the
* same type containing the members of the given arr which satisfy the
* given predicate.
*
* @param {Function} fn - predicate
* @param {Array} arr
* @return {Array}
* @example
*
* var isEven = n => n % 2 === 0;
*
* filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
*/
export default curryN(2, (fn, arr = []) => {
const result = [];
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i], i, arr)) {
result.push(arr[i]);
}
}
return result;
}) as typeof filter