array.js
3.52 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Generated by CoffeeScript 1.6.3
var array;
module.exports = array = {
/*
Tries to turn anything into an array.
*/
from: function(r) {
return Array.prototype.slice.call(r);
},
/*
Clone of an array. Properties will be shallow copies.
*/
simpleClone: function(a) {
return a.slice(0);
},
shallowEqual: function(a1, a2) {
var i, val, _i, _len;
if (!(Array.isArray(a1) && Array.isArray(a2) && a1.length === a2.length)) {
return false;
}
for (i = _i = 0, _len = a1.length; _i < _len; i = ++_i) {
val = a1[i];
if (a2[i] !== val) {
return false;
}
}
return true;
},
pluck: function(a, i) {
var index, value, _i, _len;
if (a.length < 1) {
return a;
}
for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
value = a[index];
if (index > i) {
a[index - 1] = a[index];
}
}
a.length = a.length - 1;
return a;
},
pluckItem: function(a, item) {
var index, removed, value, _i, _len;
if (a.length < 1) {
return a;
}
removed = 0;
for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
value = a[index];
if (value === item) {
removed++;
continue;
}
if (removed !== 0) {
a[index - removed] = a[index];
}
}
if (removed > 0) {
a.length = a.length - removed;
}
return a;
},
pluckOneItem: function(a, item) {
var index, reached, value, _i, _len;
if (a.length < 1) {
return a;
}
reached = false;
for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
value = a[index];
if (!reached) {
if (value === item) {
reached = true;
continue;
}
} else {
a[index - 1] = a[index];
}
}
if (reached) {
a.length = a.length - 1;
}
return a;
},
pluckByCallback: function(a, cb) {
var index, removed, value, _i, _len;
if (a.length < 1) {
return a;
}
removed = 0;
for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
value = a[index];
if (cb(value, index)) {
removed++;
continue;
}
if (removed !== 0) {
a[index - removed] = a[index];
}
}
if (removed > 0) {
a.length = a.length - removed;
}
return a;
},
pluckMultiple: function(array, indexesToRemove) {
var i, removedSoFar, _i, _len;
if (array.length < 1) {
return array;
}
removedSoFar = 0;
indexesToRemove.sort();
for (_i = 0, _len = indexesToRemove.length; _i < _len; _i++) {
i = indexesToRemove[_i];
this.pluck(array, i - removedSoFar);
removedSoFar++;
}
return array;
},
injectByCallback: function(a, toInject, shouldInject) {
var i, len, val, valA, valB, _i, _len;
valA = null;
valB = null;
len = a.length;
if (len < 1) {
a.push(toInject);
return a;
}
for (i = _i = 0, _len = a.length; _i < _len; i = ++_i) {
val = a[i];
valA = valB;
valB = val;
if (shouldInject(valA, valB, toInject)) {
return a.splice(i, 0, toInject);
}
}
a.push(toInject);
return a;
},
injectInIndex: function(a, index, toInject) {
var i, len, toPut, toPutNext;
len = a.length;
i = index;
if (len < 1) {
a.push(toInject);
return a;
}
toPut = toInject;
toPutNext = null;
for(; i <= len; i++){
toPutNext = a[i];
a[i] = toPut;
toPut = toPutNext;
};
return null;
}
};