-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathall.ts
37 lines (30 loc) · 1.05 KB
/
all.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 { PH, Predicate1 } from './utils/types.ts';
import { dispatch } from './utils/dispatch.ts';
import AllTransformer from './utils/Transformers/all.ts';
// @types
type All_2<T> = (functor: ArrayLike<T>) => boolean;
type All_1<T> = (predicate: Predicate1<T>) => boolean;
type All =
& (<T>(predicate: Predicate1<T>) => All_2<T>)
& (<T>(predicate: PH, functor: ArrayLike<T>) => All_1<T>)
& (<T>(predicate: Predicate1<T>, functor: ArrayLike<T>) => boolean);
function _all<T>(predicate: Predicate1<T>, functor: ArrayLike<T>) {
let index = 0;
while (index < functor.length) {
if (!predicate(functor[index])) {
return false;
}
index++;
}
return true;
}
const dispatchedAll = dispatch(AllTransformer, _all);
/**
* Return `true` if all the elements of the functor match `predicate`
* `false` otherwise
*
* Acts as a transducer if a transformer is passed in place of `functor`
*/
export const all = curryN(2, dispatchedAll) as All;