-
Notifications
You must be signed in to change notification settings - Fork 4
/
array-methods.js
65 lines (50 loc) · 1.43 KB
/
array-methods.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
60
61
62
63
64
65
var ObservArray = require("./index.js")
var slice = Array.prototype.slice
var ARRAY_METHODS = [
"concat", "slice", "every", "filter", "forEach", "indexOf",
"join", "lastIndexOf", "map", "reduce", "reduceRight",
"some", "toString", "toLocaleString"
]
var methods = ARRAY_METHODS.map(function (name) {
return [name, function () {
var res = this._list[name].apply(this._list, arguments)
if (res && Array.isArray(res)) {
res = ObservArray(res)
}
return res
}]
})
module.exports = ArrayMethods
function ArrayMethods(obs) {
obs.push = observArrayPush
obs.pop = observArrayPop
obs.shift = observArrayShift
obs.unshift = observArrayUnshift
obs.reverse = require("./array-reverse.js")
obs.sort = require("./array-sort.js")
methods.forEach(function (tuple) {
obs[tuple[0]] = tuple[1]
})
return obs
}
function observArrayPush() {
var args = slice.call(arguments)
args.unshift(this._list.length, 0)
this.splice.apply(this, args)
return this._list.length
}
function observArrayPop() {
return this.splice(this._list.length - 1, 1)[0]
}
function observArrayShift() {
return this.splice(0, 1)[0]
}
function observArrayUnshift() {
var args = slice.call(arguments)
args.unshift(0, 0)
this.splice.apply(this, args)
return this._list.length
}
function notImplemented() {
throw new Error("Pull request welcome")
}