-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreject.ts
37 lines (31 loc) · 1.04 KB
/
reject.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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { InferElementType, Obj, PH, Predicate1 } from './utils/types.ts';
import { complement } from './complement.ts';
import { filter } from './filter.ts';
// @types
type Reject_2<T> = <F extends T[] | Obj<T>>(filterable: F) => F;
type Reject_1<F extends unknown[] | Obj<unknown>> = (
predicate: Predicate1<InferElementType<F>>,
) => F;
type Reject =
& (<F extends T[] | Obj<T>, T>(predicate: Predicate1<T>, filterable: F) => F)
& (<T>(predicate: Predicate1<T>) => Reject_2<T>)
& (<F extends unknown[] | Obj<unknown>>(
predicate: PH,
filterable: F,
) => Reject_1<F>);
function _reject<F extends T[] | Obj<T>, T>(
predicate: Predicate1<T>,
filterable: F,
) {
return filter(complement(predicate), filterable);
}
/**
* works as the complement of filter
*
* const isOdd = n => (n & 1) === 1;
* const f = Fae.reject(isOdd, [1, 2, 3, 4])
* f() // [2, 4]
*/
export const reject = curryN(2, _reject) as Reject;