/
all.ts
27 lines (25 loc) · 936 Bytes
/
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
/*
* @author electricessence / https://github.com/electricessence/
* @license MIT
*/
import {PredicateWithIndex} from '@tsdotnet/common-interfaces';
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
import {IterableTransform} from '../IterableTransform';
/**
* An iterable transform that resolves false if the predicate ever returns false. Otherwise true.
* @param {PredicateWithIndex<T>} predicate
* @return {IterableTransform<T, boolean>}
*/
export default function all<T> (predicate: PredicateWithIndex<T>): IterableTransform<T, boolean> {
if(!predicate) throw new ArgumentNullException('predicate');
return function(sequence: Iterable<T>): boolean {
if(!sequence) throw new ArgumentNullException('sequence');
if(sequence instanceof Array) return sequence.every(predicate);
let i = 0;
for(const e of sequence)
{
if(!predicate(e, i++)) return false;
}
return true;
};
}