-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfor-each.ts
54 lines (50 loc) · 1.38 KB
/
for-each.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { noop } from './noop'
export function forEach<T>(target: Array<T>, eachFunc: (val: T, key: number) => void, inverse?: boolean): void
export function forEach<T>(
target: {
[index: string]: T
},
eachFunc: (val: T, key: string) => void,
inverse?: boolean,
): void
export function forEach(target: any, eachFunc: (val: any, key: any) => void, inverse?: boolean): void
export function forEach(target: any, eachFunc: (val: any, key: any) => any, inverse?: boolean): void {
let length: number
let handler = eachFunc
if (target instanceof Set || target instanceof Map) {
// since we cannot use [[Set/Map]].entries() directly
;(target as any).forEach((value: any, key: any) => {
if (handler(value, key) === false) {
handler = noop
}
})
} else if (target instanceof Array) {
length = target.length
if (!inverse) {
let i = -1
while (++i < length) {
if (eachFunc(target[i], i) === false) {
break
}
}
} else {
let i = length
while (i--) {
if (eachFunc(target[i], i) === false) {
break
}
}
}
} else if (typeof target === 'object') {
const keys = Object.keys(target)
let key: string
length = keys.length
let i = -1
while (++i < length) {
key = keys[i]
if (eachFunc(target[key], key) === false) {
break
}
}
}
}