-
Notifications
You must be signed in to change notification settings - Fork 41
/
turing.enumerable.js
391 lines (368 loc) · 13.4 KB
/
turing.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
/*!
* Turing Enumerable
* Copyright (C) 2010-2011 Alex R. Young
* MIT Licensed
*/
/**
* The Turing Enumerable module.
*
* This is bound to DOM objects:
*
* global('p').each(function() {
* // `this` contains a DOM element
* });
*
*/
define('turing.enumerable', ['turing.core'], function(turing) {
function EnumerableModule(global) {
global.enumerable = {
/**
* Throw to break out of iterators.
*/
Break: {},
/**
* Iterates using a function over a set of items. Example:
*
* turing.enumerable.each([1, 2, 3], function(n) {
* console.log(n);
* });
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback The function to run
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Object} The passed in enumerable object
*/
each: function(enumerable, callback, context) {
try {
if (Array.prototype.forEach && enumerable.forEach === Array.prototype.forEach) {
enumerable.forEach(callback, context);
} else if (global.isNumber(enumerable.length)) {
for (var i = 0, l = enumerable.length; i < l; i++) callback.call(enumerable, enumerable[i], i, enumerable);
} else {
for (var key in enumerable) {
if (hasOwnProperty.call(enumerable, key)) callback.call(context, enumerable[key], key, enumerable);
}
}
} catch(e) {
if (e != global.enumerable.Break) throw e;
}
return enumerable;
},
/**
* Changes a set of item using a function. Example:
*
* turing.enumerable.map([1, 2, 3], function(n) {
* return n + 1;
* });
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback The function to run over each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Array} The changed items
*/
map: function(enumerable, callback, context) {
if (Array.prototype.map && enumerable.map === Array.prototype.map) return enumerable.map(callback, context);
var results = [];
global.enumerable.each(enumerable, function(value, index, list) {
results.push(callback.call(context, value, index, list));
});
return results;
},
/**
* Removes items based on a callback. For example:
*
* var a = [1, 2, 3, 4, 5, 6, 7, 8];
* turing.enumerable.filter(a, function(n) {
* return n % 2 === 0;
* });
*
* => [2, 4, 6, 8]
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback The function to run over each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Array} The filtered items
*/
filter: function(enumerable, callback, context) {
if (Array.prototype.filter && enumerable.filter === Array.prototype.filter)
return enumerable.filter(callback, context);
var results = [],
pushIndex = !global.isArray(enumerable);
global.enumerable.each(enumerable, function(value, index, list) {
if (callback.call(context, value, index, list)) {
if (pushIndex) {
results.push([index, value]);
} else {
results.push(value);
}
}
});
return results;
},
/**
* The opposite of filter. For example:
*
* var a = [1, 2, 3, 4, 5, 6, 7, 8];
* turing.enumerable.reject(a, function(n) {
* return n % 2 === 0;
* });
*
* => [1, 3, 5, 7]
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback The function to run over each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Array} The rejected items
*/
reject: function(enumerable, callback, context) {
return this.filter(enumerable, function() {
return !callback.apply(context, arguments);
}, context);
},
/**
* Find a single item. For example:
*
* var a = [1, 2, 3, 4, 5, 6, 7, 8];
* turing.enumerable.detect(a, function(n) {
* return n === 3;
* });
*
* => 3
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback The function to run over each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Object} The item, if found
*/
detect: function(enumerable, callback, context) {
var result;
global.enumerable.each(enumerable, function(value, index, list) {
if (callback.call(context, value, index, list)) {
result = value;
throw global.enumerable.Break;
}
});
return result;
},
/**
* Runs a function over each item, collecting the results:
*
* var a = [1, 2, 3, 4, 5, 6, 7, 8];
* turing.enumerable.reduce(a, 0, function(memo, n) {
* return memo + n;
* });
*
* => 36
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Object} memo The initial accumulator value
* @param {Function} callback The function to run over each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Object} The accumulated results
*/
reduce: function(enumerable, memo, callback, context) {
if (Array.prototype.reduce && enumerable.reduce === Array.prototype.reduce)
return enumerable.reduce(global.bind(callback, context), memo);
global.enumerable.each(enumerable, function(value, index, list) {
memo = callback.call(context, memo, value, index, list);
});
return memo;
},
/**
* Flattens multidimensional arrays:
*
* turing.enumerable.flatten([[2, 4], [[6], 8]]);
*
* => [2, 4, 6, 8]
*
* @param {Object} enumerable A set of items that responds to `length`
* @returns {Object} The flat array
*/
flatten: function(array) {
return global.enumerable.reduce(array, [], function(memo, value) {
if (global.isArray(value)) return memo.concat(global.enumerable.flatten(value));
memo.push(value);
return memo;
});
},
/**
* Return the last items from a list:
*
* turing.enumerable.tail([1, 2, 3, 4, 5], 3);
*
* => [4, 5]
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Number} start The index of the item to 'cut' the array
* @returns {Object} A list of items
*/
tail: function(enumerable, start) {
start = typeof start === 'undefined' ? 1 : start;
return Array.prototype.slice.apply(enumerable, [start]);
},
/**
* Invokes `method` on a list of items:
*
* turing.enumerable.invoke(['hello', 'world'], 'substring', 0, 3);
*
* => ['hel', 'wor']
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} method The method to invoke on each item
* @returns {Object} The changed list
*/
invoke: function(enumerable, method) {
var args = global.enumerable.tail(arguments, 2);
return global.enumerable.map(enumerable, function(value) {
return (method ? value[method] : value).apply(value, args);
});
},
/**
* Pluck a property from each item of a list:
*
* turing.enumerable.pluck(['hello', 'world'], 'length');
*
* => [5, 5]
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {String} key The property to pluck
* @returns {Object} The plucked properties
*/
pluck: function(enumerable, key) {
return global.enumerable.map(enumerable, function(value) {
return value[key];
});
},
/**
* Determines if a list matches some items based on a callback:
*
* turing.enumerable.some([1, 2, 3], function(value) {
* return value === 3;
* });
*
* => true
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback A function to run against each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Boolean} True if an item was matched
*/
some: function(enumerable, callback, context) {
callback = callback || global.enumerable.identity;
if (Array.prototype.some && enumerable.some === Array.prototype.some)
return enumerable.some(callback, context);
var result = false;
global.enumerable.each(enumerable, function(value, index, list) {
if (result = callback.call(context, value, index, list)) {
throw global.enumerable.Break;
}
});
return result;
},
/**
* Checks if all items match the callback:
*
* turing.enumerable.all([1, 2, 3], function(value) {
* return value < 4;
* })
*
* => true
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Function} callback A function to run against each item
* @param {Object} [context] An optional parameter to determine `this` in the callback
* @returns {Boolean} True if all items match
*/
all: function(enumerable, callback, context) {
callback = callback || global.enumerable.identity;
if (Array.prototype.every && enumerable.every === Array.prototype.every)
return enumerable.every(callback, context);
var result = true;
global.enumerable.each(enumerable, function(value, index, list) {
if (!(result = result && callback.call(context, value, index, list))) {
throw global.enumerable.Break;
}
});
return result;
},
/**
* Checks if one item matches a value:
*
* turing.enumerable.include([1, 2, 3], 3);
*
* => true
*
* @param {Object} enumerable A set of items that responds to `length`
* @param {Object} target A value to find
* @returns {Boolean} True if an item was found
*/
include: function(enumerable, target) {
if (Array.prototype.indexOf && enumerable.indexOf === Array.prototype.indexOf)
return enumerable.indexOf(target) != -1;
var found = false;
global.enumerable.each(enumerable, function(value, key) {
if (found = value === target) {
throw global.enumerable.Break;
}
});
return found;
},
/**
* Chain enumerable calls:
*
* turing.enumerable.chain([1, 2, 3, 4])
* .filter(function(n) { return n % 2 == 0; })
* .map(function(n) { return n * 10; })
* .values();
*
* => [20, 40]
*
* @param {Object} enumerable A set of items that responds to `length`
* @returns {Object} The chained enumerable API
*/
chain: function(enumerable) {
return new global.enumerable.Chainer(enumerable);
},
identity: function(value) {
return value;
}
};
// Aliases
global.enumerable.select = global.enumerable.filter;
global.enumerable.collect = global.enumerable.map;
global.enumerable.inject = global.enumerable.reduce;
global.enumerable.rest = global.enumerable.tail;
global.enumerable.any = global.enumerable.some;
global.enumerable.every = global.enumerable.all;
global.chainableMethods = ['map', 'collect', 'detect', 'filter', 'reduce', 'each',
'tail', 'rest', 'reject', 'pluck', 'any', 'some', 'all'];
// Chainer class
global.enumerable.Chainer = function(values) {
this.results = values;
};
global.enumerable.Chainer.prototype.values = function() {
return this.results;
};
global.enumerable.each(global.chainableMethods, function(methodName) {
var method = global.enumerable[methodName];
global.enumerable.Chainer.prototype[methodName] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this.results);
this.results = method.apply(this, args);
return this;
}
});
global.init(function(arg) {
if (arg.hasOwnProperty.length && typeof arg !== 'string') {
return global.enumerable.chain(arg);
}
});
}
if (typeof module !== 'undefined') {
module.exports = function(t) {
return EnumerableModule(t);
}
} else {
EnumerableModule(turing);
}
});