-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.js
52 lines (44 loc) · 1.63 KB
/
array.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
var
EventEmitter = require('./emitter').EventEmitter,
util = require('./util').util
function array(initArr) {
initArr = initArr || []
function array(arr) {
if (arguments.length === 1) {
if (util.isArray(arr)) {
initArr = arr
array.emit('reset', initArr)
} else if (typeof arr === 'number') {
return initArr[arr]
}
} else {
return util.clone(initArr)
}
}
// Mutator method https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
'pop push reverse shift sort splice unshit'.split(' ').forEach(function (method) {
array[method] = function () {
var
args = util.arrayify(arguments),
ret = initArr[method].apply(initArr, args)
array.emit(method, args, initArr)
array.emit('change', args, initArr)
return ret
}
})
// Accessor https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
'concat join slice indexOf lastIndexOf'.split(' ').forEach(function (method) {
array[method] = function () {
return initArr[method].apply(initArr, arguments)
}
})
// Iteration https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
'forEach every some filter map reduce reduceRight'.split(' ').forEach(function (method) {
array[method] = function () {
return initArr[method].apply(initArr, arguments)
}
})
util.extend(array, EventEmitter.prototype)
return array
}
exports.array = array