-
Notifications
You must be signed in to change notification settings - Fork 184
/
shim-array.js
274 lines (239 loc) · 6.67 KB
/
shim-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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use strict";
/*
Based in part on extras from Motorola Mobility’s Montage
Copyright (c) 2012, Motorola Mobility LLC. All Rights Reserved.
3-Clause BSD License
https://github.com/motorola-mobility/montage/blob/master/LICENSE.md
*/
var Function = require("./shim-function");
var GenericCollection = require("./generic-collection");
var GenericOrder = require("./generic-order");
var WeakMap = require("weak-map");
module.exports = Array;
Array.empty = [];
if (Object.freeze) {
Object.freeze(Array.empty);
}
Array.from = function (values) {
var array = [];
array.addEach(values);
return array;
};
Array.unzip = function (table) {
var transpose = [];
var length = Infinity;
// compute shortest row
for (var i = 0; i < table.length; i++) {
var row = table[i];
table[i] = row.toArray();
if (row.length < length) {
length = row.length;
}
}
for (var i = 0; i < table.length; i++) {
var row = table[i];
for (var j = 0; j < row.length; j++) {
if (j < length && j in row) {
transpose[j] = transpose[j] || [];
transpose[j][i] = row[j];
}
}
}
return transpose;
};
function define(key, value) {
Object.defineProperty(Array.prototype, key, {
value: value,
writable: true,
configurable: true,
enumerable: false
});
}
define("addEach", GenericCollection.prototype.addEach);
define("deleteEach", GenericCollection.prototype.deleteEach);
define("toArray", GenericCollection.prototype.toArray);
define("toObject", GenericCollection.prototype.toObject);
define("all", GenericCollection.prototype.all);
define("any", GenericCollection.prototype.any);
define("min", GenericCollection.prototype.min);
define("max", GenericCollection.prototype.max);
define("sum", GenericCollection.prototype.sum);
define("average", GenericCollection.prototype.average);
define("only", GenericCollection.prototype.only);
define("flatten", GenericCollection.prototype.flatten);
define("zip", GenericCollection.prototype.zip);
define("enumerate", GenericCollection.prototype.enumerate);
define("group", GenericCollection.prototype.group);
define("sorted", GenericCollection.prototype.sorted);
define("reversed", GenericCollection.prototype.reversed);
define("constructClone", function (values) {
var clone = new this.constructor();
clone.addEach(values);
return clone;
});
define("has", function (value, equals) {
return this.find(value, equals) !== -1;
});
define("get", function (index, defaultValue) {
if (+index !== index)
throw new Error("Indicies must be numbers");
if (!index in this) {
return defaultValue;
} else {
return this[index];
}
});
define("set", function (index, value) {
this.splice(index, 1, value);
return true;
});
define("add", function (value) {
this.push(value);
return true;
});
define("delete", function (value, equals) {
var index = this.find(value, equals);
if (index !== -1) {
this.splice(index, 1);
return true;
}
return false;
});
define("find", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
for (var index = 0; index < this.length; index++) {
if (index in this && equals(this[index], value)) {
return index;
}
}
return -1;
});
define("findLast", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
var index = this.length;
do {
index--;
if (index in this && equals(this[index], value)) {
return index;
}
} while (index > 0);
return -1;
});
define("swap", function (index, length, plus) {
var args = Array.prototype.slice.call(arguments, 0, 2);
if (plus) {
if (!Array.isArray(plus)) {
plus = Array.prototype.slice.call(plus);
}
args.push.apply(args, plus);
}
return this.splice.apply(this, args);
});
define("one", function () {
for (var i in this) {
if (Object.owns(this, i)) {
return this[i];
}
}
});
define("clear", function () {
this.length = 0;
return this;
});
define("compare", function (that, compare) {
compare = compare || Object.compare;
var i;
var length;
var lhs;
var rhs;
var relative;
if (this === that) {
return 0;
}
if (!that || !Array.isArray(that)) {
return GenericOrder.prototype.compare.call(this, that, compare);
}
length = Math.min(this.length, that.length);
for (i = 0; i < length; i++) {
if (i in this) {
if (!(i in that)) {
return -1;
} else {
lhs = this[i];
rhs = that[i];
relative = compare(lhs, rhs);
if (relative) {
return relative;
}
}
} else if (i in that) {
return 1;
}
}
return this.length - that.length;
});
define("equals", function (that, equals) {
equals = equals || Object.equals;
var i = 0;
var length = this.length;
var left;
var right;
if (this === that) {
return true;
}
if (!that || !Array.isArray(that)) {
return GenericOrder.prototype.equals.call(this, that);
}
if (length !== that.length) {
return false;
} else {
for (; i < length; ++i) {
if (i in this) {
if (!(i in that)) {
return false;
}
left = this[i];
right = that[i];
if (!equals(left, right)) {
return false;
}
} else {
if (i in that) {
return false;
}
}
}
}
return true;
});
define("clone", function (depth, memo) {
if (depth === undefined) {
depth = Infinity;
} else if (depth === 0) {
return this;
}
memo = memo || new WeakMap();
var clone = [];
for (var i in this) {
if (Object.owns(this, i)) {
clone[i] = Object.clone(this[i], depth - 1, memo);
}
};
return clone;
});
define("iterate", function (start, end) {
return new ArrayIterator(this, start, end);
});
define("Iterator", ArrayIterator);
function ArrayIterator(array, start, end) {
this.array = array;
this.start = start == null ? 0 : start;
this.end = end;
};
ArrayIterator.prototype.next = function () {
if (this.start === (this.end == null ? this.array.length : this.end)) {
throw StopIteration;
} else {
return this.array[this.start++];
}
};