-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (55 loc) · 1.21 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
55
56
57
58
59
(function () {
const methodsOfArray = [
"concat",
"copyWithin",
"entries",
"every",
"fill",
"filter",
"find",
"findIndex",
"flat",
"flatMap",
"forEach",
"includes",
"indexOf",
"join",
"keys",
"lastIndexOf",
"map",
"pop",
"push",
"reduce",
"reduceRight",
"reverse",
"shift",
"slice",
"some",
"sort",
"splice",
"toLocaleString",
"toString",
"unshift",
"values",
]
function proxyArrayFunc (arrayFuncName) {
return function (func) {
// valueOf is used to get primitive value of boolean.
// Date.prototype.valueOf() return a timestamp. It's not what we want.
const copy = isDate(this) ? this : this.valueOf()
const result = [copy][arrayFuncName](func)
if (!result) return undefined
if (result.length > 1) return result
return result[0]
}
}
function isDate (value) {
return value instanceof Date
}
const newProperties = methodsOfArray.reduce((obj, methodName) => {
obj[methodName] = { value: proxyArrayFunc(methodName) }
return obj
}, {})
/* eslint no-extend-native: off */
Object.defineProperties(Object.prototype, newProperties)
})()