/
every.ts
38 lines (36 loc) 路 990 Bytes
/
every.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
38
import type {
Predicate,
ReadOnlyIterable,
Single,
Unary,
} from "@lou.codes/types";
/**
* Evaluates items in an iterable against a predicate and returns `true` if all
* items evaluates to `true`.
*
* @category Reducers
* @example
* ```typescript
* const everyEven = every((number: number) => number % 2 === 0);
* everyEven([2, 4, 6, 8]); // true
* everyEven([1, 2, 3, 4]); // false
* ```
* @param predicate Predicate function to evaluate each item.
* @returns Curried function with `predicate` set in context.
*/
export const every =
<Item, Predicated extends Item = never>(
predicate: Single<Predicated> extends Single<never> ?
Unary<Item, boolean>
: Predicate<Item, Predicated>,
) =>
(iterable: ReadOnlyIterable<Item>) => {
// eslint-disable-next-line functional/no-loop-statements
for (const item of iterable) {
// eslint-disable-next-line functional/no-conditional-statements
if (!predicate(item)) {
return false;
}
}
return true;
};