-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevery.ts
30 lines (27 loc) · 1.5 KB
/
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
import { identity } from '../util/identity';
import { iteratee } from '../util/iteratee';
import { isArray } from '../lang/isArray';
import { isString } from '../lang/isString';
import { entries } from '../object/entries';
type PropertyName = string | number | symbol;
type IterateeShorthand<T> = PropertyName | [PropertyName, any] | Partial<T>;
type ArrayIterator<T, R> = (value: T, index: number, collection: T[]) => R;
type ArrayIterateeCustom<T, R> = ArrayIterator<T, R> | IterateeShorthand<T>;
type StringIterator<R> = (char: string, index: number, string: string) => R;
type StringIterateeCustom<R> = StringIterator<R> | IterateeShorthand<string>;
type RecordIterator<K extends PropertyName, V, R> = (value: V, key: K, collection: Record<K, V>) => R;
type RecordIterateeCustom<K extends PropertyName, V, R> = RecordIterator<K, V, R> | IterateeShorthand<V>;
function every<T>(collection: T[], predicate?: ArrayIterateeCustom<T, boolean>): boolean;
function every(collection: string, predicate?: StringIterateeCustom<boolean>): boolean;
function every<K extends PropertyName, V>(
collection: Record<K, V>,
predicate?: RecordIterateeCustom<K, V, boolean>
): boolean;
function every(collection: any, predicate: any = identity): boolean {
const iterativeFunc = iteratee(predicate);
return entries(collection).every(([key, value]: [PropertyName, unknown]) => {
key = isArray(collection) || isString(collection) ? Number(key) : key;
return iterativeFunc(value, key, collection);
});
}
export { every };