forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim-array.js
463 lines (410 loc) · 12.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"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;
var array_splice = Array.prototype.splice;
var array_slice = Array.prototype.slice;
Array.empty = [];
if (Object.freeze) {
Object.freeze(Array.empty);
}
Array.nativeFrom = Array.from;
var isSymbolDefined = typeof Symbol !== "undefined";
Array.from = function (values, mapFn, thisArg) {
if (isSymbolDefined && values && typeof values[Symbol.iterator] === "function") {
return Array.nativeFrom(values, mapFn, thisArg);
}
//Now we add support for values that implement forEach:
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.findValue(value, equals) !== -1;
});
define("get", function (index, defaultValue) {
if (+index !== index) {
throw new Error("Indicies must be numbers");
} else if (!index in this) {
return defaultValue;
} else {
return this[index];
}
});
define("set", function (index, value) {
this[index] = value;
return true;
});
define("add", function (value) {
this.push(value);
return true;
});
define("delete", function (value, equals) {
var index = this.findValue(value, equals);
if (index !== -1) {
this.spliceOne(index);
return true;
}
return false;
});
define("deleteAll", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
var count = 0;
for (var index = 0; index < this.length;) {
if (equals(value, this[index])) {
this.swap(index, 1);
count++;
} else {
index++;
}
}
return count;
});
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
define("find", function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
});
}
// TODO remove in v6 (not present in v2)
var deprecatedWarnNonce = {};
function deprecatedWarn(msg, notOnce) {
if (
typeof console !== 'undefined' &&
typeof console.warn === 'function' &&
(notOnce !== true && deprecatedWarnNonce.hasOwnProperty(msg) === false)
) {
console.warn(msg);
deprecatedWarnNonce[msg]++;
}
}
// Save Array.prototype.find in order to support legacy and display warning.
// TODO remove in v6 (not present in v2)
var ArrayFindPrototype = Object.getOwnPropertyDescriptor(Array.prototype, 'find').value;
define("find", function (value, equals, index) {
if (
typeof arguments[0] === 'function' &&
this instanceof Array
) {
return ArrayFindPrototype.apply(this, arguments);
} else {
deprecatedWarn('Array#find usage is deprecated please use Array#findValue');
return this.findValue.apply(this, arguments);
}
});
define("findValue", function (value, equals, index) {
if (index) {
throw new Error("Array#findValue does not support third argument: index");
}
equals = equals || this.contentEquals || Object.equals;
for (var index = 0; index < this.length; index++) {
if (index in this && equals(value, this[index])) {
return index;
}
}
return -1;
});
// TODO remove in v6 (not present in v2)
define("findLast", function (value, equals) {
deprecatedWarn('Array#findLast function is deprecated please use Array#findLastValue instead.');
return this.findLastValue.apply(this, arguments);
});
define("findLastValue", 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 (start, length, plus) {
var args, plusLength, i, j, returnValue;
if (start > this.length) {
this.length = start;
}
if (typeof plus !== "undefined") {
args = [start, length];
if (!Array.isArray(plus)) {
plus = array_slice.call(plus);
}
i = 0;
plusLength = plus.length;
// 1000 is a magic number, presumed to be smaller than the remaining
// stack length. For swaps this small, we take the fast path and just
// use the underlying Array splice. We could measure the exact size of
// the remaining stack using a try/catch around an unbounded recursive
// function, but this would defeat the purpose of short-circuiting in
// the common case.
if (plusLength < 1000) {
for (i; i < plusLength; i++) {
args[i+2] = plus[i];
}
return array_splice.apply(this, args);
} else {
// Avoid maximum call stack error.
// First delete the desired entries.
returnValue = array_splice.apply(this, args);
// Second batch in 1000s.
for (i; i < plusLength;) {
args = [start+i, 0];
for (j = 2; j < 1002 && i < plusLength; j++, i++) {
args[j] = plus[i];
}
array_splice.apply(this, args);
}
return returnValue;
}
// using call rather than apply to cut down on transient objects
} else if (typeof length !== "undefined") {
return array_splice.call(this, start, length);
} else if (typeof start !== "undefined") {
return array_splice.call(this, start);
} else {
return [];
}
});
define("peek", function () {
return this[0];
});
define("poke", function (value) {
if (this.length > 0) {
this[0] = value;
}
});
define("peekBack", function () {
if (this.length > 0) {
return this[this.length - 1];
}
});
define("pokeBack", function (value) {
if (this.length > 0) {
this[this.length - 1] = value;
}
});
define("one", function () {
for (var i in this) {
if (Object.owns(this, i)) {
return this[i];
}
}
});
if (!Array.prototype.clear) {
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 = (this.length < that.length) ? 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 == null) {
depth = Infinity;
} else if (depth === 0) {
return this;
}
memo = memo || new WeakMap();
if (memo.has(this)) {
return memo.get(this);
}
var clone = new Array(this.length);
memo.set(this, clone);
for (var i in this) {
clone[i] = Object.clone(this[i], depth - 1, memo);
};
return clone;
});
define("iterate", function (start, end) {
return new ArrayIterator(this, start, end);
});
if(Array.prototype.spliceOne === void 0) {
define("spliceOne", function (index,itemToAdd) {
var len=this.length;
if (!len) { return }
if(arguments.length === 1) {
while (index<len) {
this[index] = this[index+1];
index++
}
this.length--;
}
else {
this[index] = itemToAdd;
}
});
}
define("Iterator", ArrayIterator);
function ArrayIterator(array, start, end) {
this.array = array;
this.start = start == null ? 0 : start;
this.end = end;
}
ArrayIterator.prototype.__iterationObject = null;
Object.defineProperty(ArrayIterator.prototype,"_iterationObject", {
get: function() {
return this.__iterationObject || (this.__iterationObject = { done: false, value:null});
}
});
ArrayIterator.prototype.next = function () {
if (this.start === (this.end == null ? this.array.length : this.end)) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
} else {
this._iterationObject.value = this.array[this.start++];
}
return this._iterationObject;
};