-
Notifications
You must be signed in to change notification settings - Fork 184
/
generic-collection.js
324 lines (290 loc) · 9.44 KB
/
generic-collection.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
"use strict";
module.exports = GenericCollection;
function GenericCollection() {
throw new Error("Can't construct. GenericCollection is a mixin.");
}
var DOMTokenList = global.DOMTokenList || function(){};
GenericCollection.EmptyArray = Object.freeze([]);
GenericCollection.prototype.addEach = function (values, mapFn, thisp) {
//We want to eliminate everything but array like: Strings, Arrays, DOMTokenList
if(values && (values instanceof Array || (values instanceof DOMTokenList) || values instanceof String)) {
if(mapFn) {
for (var i = 0; i < values.length; i++) {
this.add(mapFn.call(thisp,values[i]), i);
}
} else {
for (var i = 0; i < values.length; i++) {
this.add(values[i], i);
}
}
}
else if (values && Object(values) === values) {
if (typeof values.forEach === "function") {
if(mapFn) {
values.map(mapFn, thisp).forEach(this.add, this);
} else {
values.forEach(this.add, this);
}
} else if(typeof values.next === "function") {
var value, i=0;
if(mapFn) {
while ((value = values.next().value)) {
this.add(mapFn.call(thisp,value), i++);
}
}
else {
while ((value = values.next().value)) {
this.add(value, i++);
}
}
} else if (typeof values.length === "number") {
// Array-like objects that do not implement forEach, ergo,
// Arguments
if(mapFn) {
for (var i = 0; i < values.length; i++) {
this.add(mapFn.call(thisp,values[i]), i);
}
}
else {
for (var i = 0; i < values.length; i++) {
this.add(values[i], i);
}
}
} else {
if(mapFn) {
Object.keys(values).forEach(function (key) {
this.add(mapFn.call(thisp,values[key]), key);
}, this);
} else {
Object.keys(values).forEach(function (key) {
this.add(values[key], key);
}, this);
}
}
}
return this;
};
// This is sufficiently generic for Map (since the value may be a key)
// and ordered collections (since it forwards the equals argument)
GenericCollection.prototype.deleteEach = function (values, equals) {
values.forEach(function (value) {
this["delete"](value, equals);
}, this);
return this;
};
// all of the following functions are implemented in terms of "reduce".
// some need "constructClone".
GenericCollection.prototype.forEach = function (callback /*, thisp*/) {
var thisp = arguments[1];
return this.reduce(function (undefined, value, key, object, depth) {
callback.call(thisp, value, key, object, depth);
}, undefined);
};
GenericCollection.prototype.map = function (callback /*, thisp*/) {
var thisp = arguments[1];
var result = [];
this.reduce(function (undefined, value, key, object, depth) {
result.push(callback.call(thisp, value, key, object, depth));
}, undefined);
return result;
};
GenericCollection.prototype.enumerate = function (start) {
if (start == null) {
start = 0;
}
var result = [];
this.reduce(function (undefined, value) {
result.push([start++, value]);
}, undefined);
return result;
};
GenericCollection.prototype.group = function (callback, thisp, equals) {
equals = equals || Object.equals;
var groups = [];
var keys = [];
this.forEach(function (value, key, object) {
var key = callback.call(thisp, value, key, object);
var index = keys.indexOf(key, equals);
var group;
if (index === -1) {
group = [];
groups.push([key, group]);
keys.push(key);
} else {
group = groups[index][1];
}
group.push(value);
});
return groups;
};
GenericCollection.prototype.toArray = function () {
return this.map(Function.identity);
};
// this depends on stringable keys, which apply to Array and Iterator
// because they have numeric keys and all Maps since they may use
// strings as keys. List, Set, and SortedSet have nodes for keys, so
// toObject would not be meaningful.
GenericCollection.prototype.toObject = function () {
var object = {};
this.reduce(function (undefined, value, key) {
object[key] = value;
}, undefined);
return object;
};
GenericCollection.from = function () {
return this.apply(this,arguments);
};
GenericCollection.prototype.filter = function (callback /*, thisp*/) {
var thisp = arguments[1];
var result = this.constructClone();
this.reduce(function (undefined, value, key, object, depth) {
if (callback.call(thisp, value, key, object, depth)) {
result.add(value, key);
}
}, undefined);
return result;
};
GenericCollection.prototype.every = function (callback /*, thisp*/) {
var thisp = arguments[1];
return this.reduce(function (result, value, key, object, depth) {
return result && callback.call(thisp, value, key, object, depth);
}, true);
};
GenericCollection.prototype.some = function (callback /*, thisp*/) {
var thisp = arguments[1];
return this.reduce(function (result, value, key, object, depth) {
return result || callback.call(thisp, value, key, object, depth);
}, false);
};
GenericCollection.prototype.all = function () {
return this.every(Boolean);
};
GenericCollection.prototype.any = function () {
return this.some(Boolean);
};
GenericCollection.prototype.min = function (compare) {
compare = compare || this.contentCompare || Object.compare;
var first = true;
return this.reduce(function (result, value) {
if (first) {
first = false;
return value;
} else {
return compare(value, result) < 0 ? value : result;
}
}, undefined);
};
GenericCollection.prototype.max = function (compare) {
compare = compare || this.contentCompare || Object.compare;
var first = true;
return this.reduce(function (result, value) {
if (first) {
first = false;
return value;
} else {
return compare(value, result) > 0 ? value : result;
}
}, undefined);
};
GenericCollection.prototype.sum = function (zero) {
zero = zero === undefined ? 0 : zero;
return this.reduce(function (a, b) {
return a + b;
}, zero);
};
GenericCollection.prototype.average = function (zero) {
var sum = zero === undefined ? 0 : zero;
var count = zero === undefined ? 0 : zero;
this.reduce(function (undefined, value) {
sum += value;
count += 1;
}, undefined);
return sum / count;
};
GenericCollection.prototype.concat = function () {
var result = this.constructClone(this);
for (var i = 0; i < arguments.length; i++) {
result.addEach(arguments[i]);
}
return result;
};
GenericCollection.prototype.flatten = function () {
var self = this;
return this.reduce(function (result, array) {
array.forEach(function (value) {
this.push(value);
}, result, self);
return result;
}, []);
};
GenericCollection.prototype.zip = function () {
var table = Array.prototype.slice.call(arguments);
table.unshift(this);
return Array.unzip(table);
}
GenericCollection.prototype.join = function (delimiter) {
return this.reduce(function (result, string) {
// work-around for reduce that does not support no-basis form
if (result === void 0) {
return string;
} else {
return result + delimiter + string;
}
}, void 0);
};
GenericCollection.prototype.sorted = function (compare, by, order) {
compare = compare || this.contentCompare || Object.compare;
// account for comparators generated by Function.by
if (compare.by) {
by = compare.by;
compare = compare.compare || this.contentCompare || Object.compare;
} else {
by = by || Function.identity;
}
if (order === undefined)
order = 1;
return this.map(function (item) {
return {
by: by(item),
value: item
};
})
.sort(function (a, b) {
return compare(a.by, b.by) * order;
})
.map(function (pair) {
return pair.value;
});
};
GenericCollection.prototype.reversed = function () {
return this.constructClone(this).reverse();
};
GenericCollection.prototype.clone = function (depth, memo) {
if (depth === undefined) {
depth = Infinity;
} else if (depth === 0) {
return this;
}
var clone = this.constructClone();
this.forEach(function (value, key) {
clone.add(Object.clone(value, depth - 1, memo), key);
}, this);
return clone;
};
GenericCollection.prototype.only = function () {
if (this.length === 1) {
return this.one();
}
};
GenericCollection.prototype.iterator = function () {
return this.iterate.apply(this, arguments);
};
GenericCollection._sizePropertyDescriptor = {
get: function() {
return this.length;
},
enumerable: false,
configurable: true
};
Object.defineProperty(GenericCollection.prototype,"size",GenericCollection._sizePropertyDescriptor);
require("./shim-array");