-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (35 loc) · 1.04 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
function addArrayMethods (customMethods, options) {
options = options || {}
var method
var allMethods = {}
var nativeMethods = options.natives || addArrayMethods.natives || []
for (var i = 0; i < nativeMethods.length; i++) {
method = nativeMethods[i]
if (!(method in Array.prototype)) continue
allMethods[method] = addArrayMethods[method] || (addArrayMethods[method] = new Function(
'return this._addArrayMethods(Array.prototype.' + method + '.apply(this, arguments))'
))
}
for (method in customMethods) {
allMethods[method] = customMethods[method]
}
return function adder (arr) {
allMethods._addArrayMethods = adder
for (method in allMethods) {
if (options.es3) {
arr[method] = allMethods[method]
} else {
Object.defineProperty(arr, method, {
value: allMethods[method]
})
}
}
return arr
}
}
addArrayMethods.natives = [
'filter', 'slice', 'concat',
'reverse', 'sort', 'splice',
'map', 'fill', 'copyWithin'
]
module.exports = addArrayMethods