-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevery.ts
39 lines (31 loc) · 1.13 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
31
32
33
34
35
36
37
38
39
import {baseIteratee} from './internal/baseIteratee'
import type {ListIterateeCustom, ObjectIterateeCustom} from './internal/baseIteratee.type'
import type {List} from './internal/types'
export function every<T>(collection: List<T> | null | undefined, predicate?: ListIterateeCustom<T, boolean>): boolean
export function every<T extends object>(
collection: T | null | undefined,
predicate?: ObjectIterateeCustom<T, boolean>,
): boolean
export function every<T, U extends T[keyof T]>(
collection: T | List<U> | null | undefined,
predicate: ListIterateeCustom<U, boolean> | ObjectIterateeCustom<T, boolean> = Boolean,
): boolean {
if (!collection) {
return true
}
const values = Object.values(collection)
const length = values.length
if (length === 0) {
return true
}
const iteratee = baseIteratee<unknown, boolean>(predicate)
for (let i = 0; i < length; i++) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!iteratee(values[i], i, collection)) {
return false
}
}
return true
}
export default every