-
Notifications
You must be signed in to change notification settings - Fork 639
/
enumerable.js
496 lines (460 loc) · 14.8 KB
/
enumerable.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/** section: Language
* mixin Enumerable
*
* `Enumerable` provides a large set of useful methods for enumerations —
* objects that act as collections of values. It is a cornerstone of
* Prototype.
*
* `Enumerable` is a _mixin_: a set of methods intended not for standaone
* use, but for incorporation into other objects.
*
* Prototype mixes `Enumerable` into several classes. The most visible cases
* are [[Array]] and [[Hash]], but you'll find it in less obvious spots as
* well, such as in [[ObjectRange]] and various DOM- or Ajax-related objects.
*
* <h4>The <code>context</code> parameter</h4>
*
* Every method of `Enumerable` that takes an iterator also takes the "context
* object" as the next (optional) parameter. The context object is what the
* iterator will be _bound_ to — what the keyword `this` will refer to inside
* the iterator.
*
* var myObject = {};
*
* ['foo', 'bar', 'baz'].each(function(name, index) {
* this[name] = index;
* }, myObject); // we have specified the context
*
* myObject
* //-> { foo: 0, bar: 1, baz: 2}
*
* If there is no `context` argument, the iterator function will execute in
* the scope from which the `Enumerable` method itself was called.
*
* <h4>Mixing <code>Enumerable</code> into your own objects</h4>
*
* So, let's say you've created your very own collection-like object (say,
* some sort of Set, or perhaps something that dynamically fetches data
* ranges from the server side, lazy-loading style). You want to be able to
* mix `Enumerable` in (and we commend you for it). How do you go about this?
*
* The Enumerable module basically makes only one requirement on your object:
* it must provide a method named `_each` (note the leading underscore) that
* will accept a function as its unique argument, and will contain the actual
* "raw iteration" algorithm, invoking its argument with each element in turn.
*
* As detailed in the documentation for [[Enumerable#each]], `Enumerable`
* provides all the extra layers (handling iteration short-circuits, passing
* numeric indices, etc.). You just need to implement the actual iteration,
* as fits your internal structure.
*
* If you're still confused, just have a look at the Prototype source code for
* [[Array]], [[Hash]], or [[ObjectRange]]. They all begin with their own
* `_each` method, which should help you grasp the idea.
*
* Once you're done with this, you just need to mix `Enumerable` in, which
* you'll usually do before defining your methods, so as to make sure whatever
* overrides you provide for `Enumerable` methods will indeed prevail. In
* short, your code will probably end up looking like this:
*
*
* var YourObject = Class.create(Enumerable, {
* initialize: function() { // with whatever constructor arguments you need
* // Your construction code
* },
*
* _each: function(iterator) {
* // Your iteration code, invoking iterator at every turn
* },
*
* // Your other methods here, including Enumerable overrides
* });
*
* Then, obviously, your object can be used like this:
*
* var obj = new YourObject();
* // Populate the collection somehow
* obj.pluck('somePropName');
* obj.invoke('someMethodName');
* obj.size();
* // etc.
*
**/
var $break = { };
var Enumerable = (function() {
/**
* Enumerable#each(iterator[, context]) -> Enumerable
* - iterator (Function): A `Function` that expects an item in the
* collection as the first argument and a numerical index as the second.
* - context (Object): The scope in which to call `iterator`. Affects what
* the keyword `this` means inside `iterator`.
*
* Calls `iterator` for each item in the collection.
**/
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
iterator.call(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}
/**
* Enumerable#eachSlice(number[, iterator = Prototype.K[, context]]) -> Enumerable
*
* Groups items into chunks of the given size.
* The final "slice" may have fewer than `number` items; it won't "pad" the
* last group with empty values. For that behavior, use
* [[Enumerable#inGroupsOf]].
**/
function eachSlice(number, iterator, context) {
var index = -number, slices = [], array = this.toArray();
if (number < 1) return array;
while ((index += number) < array.length)
slices.push(array.slice(index, index+number));
return slices.collect(iterator, context);
}
/**
* Enumerable#all([iterator = Prototype.K[, context]]) -> Boolean
*
* Determines whether all the elements are boolean-equivalent to `true`,
* either directly or through computation by the provided iterator.
**/
function all(iterator, context) {
iterator = iterator || Prototype.K;
var result = true;
this.each(function(value, index) {
result = result && !!iterator.call(context, value, index);
if (!result) throw $break;
});
return result;
}
/**
* Enumerable#any([iterator = Prototype.K[, context]]) -> Boolean
*
* Determines whether at least one element is boolean-equivalent to `true`,
* either directly or through computation by the provided iterator.
**/
function any(iterator, context) {
iterator = iterator || Prototype.K;
var result = false;
this.each(function(value, index) {
if (result = !!iterator.call(context, value, index))
throw $break;
});
return result;
}
/**
* Enumerable#collect([iterator = Prototype.K[, context]]) -> Array
*
* Returns the results of applying the iterator to each element.
* Aliased as [[Enumerable#map]].
**/
function collect(iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
this.each(function(value, index) {
results.push(iterator.call(context, value, index));
});
return results;
}
/**
* Enumerable#detect(iterator[, context]) -> firstElement | undefined
*
* Finds the first element for which the iterator returns a "truthy" value.
* Aliased by the [[Enumerable#find]] method.
**/
function detect(iterator, context) {
var result;
this.each(function(value, index) {
if (iterator.call(context, value, index)) {
result = value;
throw $break;
}
});
return result;
}
/**
* Enumerable#findAll(iterator[, context]) -> Array
*
* Returns all the elements for which the iterator returned "truthy" value.
* Aliased as [[Enumerable#select]].
**/
function findAll(iterator, context) {
var results = [];
this.each(function(value, index) {
if (iterator.call(context, value, index))
results.push(value);
});
return results;
}
/**
* Enumerable#grep(regex[, iterator = Prototype.K[, context]]) -> Array
*
* Returns all the elements that match the filter. If an iterator is provided,
* it is used to produce the returned value for each selected element.
**/
function grep(filter, iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
if (Object.isString(filter))
filter = new RegExp(RegExp.escape(filter));
this.each(function(value, index) {
if (filter.match(value))
results.push(iterator.call(context, value, index));
});
return results;
}
/**
* Enumerable#include(object) -> Boolean
*
* Determines whether a given object is in the Enumerable or not,
* based on the `==` comparison operator. Aliased as [[Enumerable#member]].
**/
function include(object) {
if (Object.isFunction(this.indexOf))
if (this.indexOf(object) != -1) return true;
var found = false;
this.each(function(value) {
if (value == object) {
found = true;
throw $break;
}
});
return found;
}
/**
* Enumerable#inGroupsOf(size[, filler = null]) -> [group...]
*
* Groups items in fixed-size chunks, using a specific value to fill up
* the last chunk if necessary.
**/
function inGroupsOf(number, fillWith) {
fillWith = Object.isUndefined(fillWith) ? null : fillWith;
return this.eachSlice(number, function(slice) {
while(slice.length < number) slice.push(fillWith);
return slice;
});
}
/**
* Enumerable#inject(accumulator, iterator[, context]) -> accumulatedValue
*
* Incrementally builds a result value based on the successive results
* of the iterator.
* This can be used for array construction, numerical sums/averages, etc.
**/
function inject(memo, iterator, context) {
this.each(function(value, index) {
memo = iterator.call(context, memo, value, index);
});
return memo;
}
/**
* Enumerable#invoke(methodName[, arg...]) -> Array
*
* Invokes the same method, with the same arguments, for all items in a collection.
* Returns the results of the method calls.
**/
function invoke(method) {
var args = $A(arguments).slice(1);
return this.map(function(value) {
return value[method].apply(value, args);
});
}
/**
* Enumerable#max([iterator = Prototype.K[, context]]) -> maxValue
*
* Returns the maximum element (or element-based computation), or undefined if
* the enumeration is empty.
* Elements are either compared directly, or by first applying the iterator
* and comparing returned values.
**/
function max(iterator, context) {
iterator = iterator || Prototype.K;
var result;
this.each(function(value, index) {
value = iterator.call(context, value, index);
if (result == null || value >= result)
result = value;
});
return result;
}
/**
* Enumerable#min([iterator = Prototype.K[, context]]) -> minValue
*
* Returns the minimum element (or element-based computation), or undefined if
* the enumeration is empty.
* Elements are either compared directly, or by first applying the iterator
* and comparing returned values.
**/
function min(iterator, context) {
iterator = iterator || Prototype.K;
var result;
this.each(function(value, index) {
value = iterator.call(context, value, index);
if (result == null || value < result)
result = value;
});
return result;
}
/**
* Enumerable#partition([iterator = Prototype.K[, context]]) -> [TrueArray, FalseArray]
*
* Partitions the elements in two groups: those regarded as true, and those
* considered false.
* By default, regular JavaScript boolean equivalence is used, but an iterator
* can be provided, that computes a boolean representation of the elements.
**/
function partition(iterator, context) {
iterator = iterator || Prototype.K;
var trues = [], falses = [];
this.each(function(value, index) {
(iterator.call(context, value, index) ?
trues : falses).push(value);
});
return [trues, falses];
}
/**
* Enumerable#pluck(propertyName) -> Array
*
* Optimization for a common use-case of collect: fetching the same property
* for all the elements. Returns the property values.
**/
function pluck(property) {
var results = [];
this.each(function(value) {
results.push(value[property]);
});
return results;
}
/**
* Enumerable#reject(iterator[, context]) -> Array
*
* Returns all the elements for which the iterator returned a "falsy" value.
**/
function reject(iterator, context) {
var results = [];
this.each(function(value, index) {
if (!iterator.call(context, value, index))
results.push(value);
});
return results;
}
/**
* Enumerable#sortBy(iterator[, context]) -> Array
*
* Provides a custom-sorted view of the elements based on the criteria computed,
* for each element, by the iterator.
**/
function sortBy(iterator, context) {
return this.map(function(value, index) {
return {
value: value,
criteria: iterator.call(context, value, index)
};
}).sort(function(left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
}).pluck('value');
}
/**
* Enumerable#toArray() -> Array
*
* Returns an Array representation of the enumeration.
**/
function toArray() {
return this.map();
}
/**
* Enumerable#zip(sequence...[, iterator = Prototype.K]) -> Array
*
* Zips together (think of the zip on a pair of trousers) 2+ sequences,
* providing an array of tuples.
* Each tuple contains one value per original sequence.
* Tuples can be converted to something else by applying the optional iterator on them.
**/
function zip() {
var iterator = Prototype.K, args = $A(arguments);
if (Object.isFunction(args.last()))
iterator = args.pop();
var collections = [this].concat(args).map($A);
return this.map(function(value, index) {
return iterator(collections.pluck(index));
});
}
/**
* Enumerable#size() -> Number
*
* Returns the size of the enumeration.
**/
function size() {
return this.toArray().length;
}
/**
* Enumerable#inspect() -> String
*
* Returns the debug-oriented string representation of the object.
**/
function inspect() {
return '#<Enumerable:' + this.toArray().inspect() + '>';
}
/** alias of: Enumerable#collect
* Enumerable#map([iterator = Prototype.K[, context]]) -> Array
**/
/** alias of: Enumerable#any
* Enumerable#some([iterator = Prototype.K[, context]]) -> Boolean
**/
/** alias of: Enumerable#all
* Enumerable#every([iterator = Prototype.K[, context]]) -> Boolean
**/
/** alias of: Enumerable#findAll
* Enumerable#select(iterator[, context]) -> Array
**/
/** alias of: Enumerable#findAll
* Enumerable#filter(iterator[, context]) -> Array
**/
/** alias of: Enumerable#include
* Enumerable#member(object) -> Boolean
**/
/** alias of: Enumerable#toArray
* Enumerable#entries() -> Array
**/
/** alias of: Enumerable#detect
* Enumerable#find(iterator[, context]) -> firstElement | undefined
**/
return {
each: each,
eachSlice: eachSlice,
all: all,
every: all,
any: any,
some: any,
collect: collect,
map: collect,
detect: detect,
findAll: findAll,
select: findAll,
filter: findAll,
grep: grep,
include: include,
member: include,
inGroupsOf: inGroupsOf,
inject: inject,
invoke: invoke,
max: max,
min: min,
partition: partition,
pluck: pluck,
reject: reject,
sortBy: sortBy,
toArray: toArray,
entries: toArray,
zip: zip,
size: size,
inspect: inspect,
find: detect
};
})();