-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallPass.ts
More file actions
26 lines (22 loc) · 811 Bytes
/
Copy pathallPass.ts
File metadata and controls
26 lines (22 loc) · 811 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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { Func, Predicate } from './utils/types.ts';
import { getFunctionsLengths } from './utils/get.ts';
/**
* Takes a list of predicates and returns a predicate that returns true for a
* given list of arguments if every one of the provided predicates is satisfied
* by those arguments.
*/
export function allPass<T>(predicates: Predicate<T>[]): Func {
const len = predicates.length;
const fn = function (this: unknown, ...args: T[]) {
for (let idx = 0; idx < len; idx++) {
if (!predicates[idx].apply(this, args)) {
return false;
}
}
return true;
};
const noOfParams = getFunctionsLengths(predicates);
return curryN(Math.max(...noOfParams, 0), fn);
}