/
index.js
54 lines (40 loc) · 1.31 KB
/
index.js
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
var isObject = obj => {
const type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
var getKeys = (obj) => {
if (!isObject(obj)) return [];
return Object.keys(obj);
};
var shallowProperty = key => obj => obj == null ? void 0 : obj[key];
var getLength = shallowProperty('length');
var isArrayLike = (collection) => {
const length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= Number.MAX_SAFE_INTEGER;
};
var optimizeCb = (func, context, argCount) => {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1: return value => func.call(context, value);
// The 2-argument case is omitted because we’re not using it.
case 3: return (value, index, collection) => func.call(context, value, index, collection);
case 4: return (accumulator, value, index, collection) => func.call(context, accumulator, value, index, collection);
}
return (...args) => func.apply(context, args);
};
var forEach = (obj, iteratee, context) => {
iteratee = optimizeCb(iteratee, context);
if (isArrayLike(obj)) {
let i = 0;
for (const item of obj) {
iteratee(item, i++, obj);
}
} else {
const keys = getKeys(obj);
for (const key of keys) {
iteratee(obj[key], key, obj);
}
}
return obj;
};
module.exports = forEach;