prototype/src/lang/enumerable.js

385 lines
11 KiB
JavaScript
Raw Normal View History

2008-12-14 04:36:59 +00:00
/** section: lang
* mixin Enumerable
**/
var $break = { };
2007-01-18 22:24:27 +00:00
2008-12-11 10:33:31 +00:00
var Enumerable = (function() {
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function each(iterator, context) {
2007-01-18 22:24:27 +00:00
var index = 0;
try {
this._each(function(value) {
iterator.call(context, value, index++);
2007-01-18 22:24:27 +00:00
});
} catch (e) {
if (e != $break) throw e;
}
return this;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#eachSlice(number[, iterator = Prototype.K[, context]]) -> Enumerable
*
* TODO: Enumerable#eachSlice
* http://prototypejs.org/api/enumerable#method-eachslice
**/
2008-12-11 10:33:31 +00:00
function eachSlice(number, iterator, context) {
2007-01-18 22:24:27 +00:00
var index = -number, slices = [], array = this.toArray();
if (number < 1) return array;
2007-01-18 22:24:27 +00:00
while ((index += number) < array.length)
slices.push(array.slice(index, index+number));
return slices.collect(iterator, context);
2008-12-11 10:33:31 +00:00
}
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function all(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var result = true;
this.each(function(value, index) {
result = result && !!iterator.call(context, value, index);
2007-01-18 22:24:27 +00:00
if (!result) throw $break;
});
return result;
2008-12-11 10:33:31 +00:00
}
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function any(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var result = false;
this.each(function(value, index) {
if (result = !!iterator.call(context, value, index))
2007-01-18 22:24:27 +00:00
throw $break;
});
return result;
2008-12-11 10:33:31 +00:00
}
2008-12-14 04:36:59 +00:00
/** alias of: Enumerable#map
* Enumerable#collect([iterator = Prototype.K[, context]]) -> Array
*
* Returns the results of applying the iterator to each element.
* Aliased as [[Enumerable#map]].
**/
2008-12-11 10:33:31 +00:00
function collect(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var results = [];
this.each(function(value, index) {
results.push(iterator.call(context, value, index));
2007-01-18 22:24:27 +00:00
});
return results;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/** alias of: Enumerable#find
* Enumerable#detect(iterator[, context]) -> firstElement | undefined
*
* Finds the first element for which the iterator returns a truthy value.
* Aliased by the [[Enumerable#find]] method.
**/
2008-12-11 10:33:31 +00:00
function detect(iterator, context) {
2007-01-18 22:24:27 +00:00
var result;
this.each(function(value, index) {
if (iterator.call(context, value, index)) {
2007-01-18 22:24:27 +00:00
result = value;
throw $break;
}
});
return result;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/** alias of: select
* Enumerable#findAll(iterator[, context]) -> Array
*
* Returns all the elements for which the iterator returned truthy value.
* Aliased as [[Enumerable#select]].
**/
2008-12-11 10:33:31 +00:00
function findAll(iterator, context) {
2007-01-18 22:24:27 +00:00
var results = [];
this.each(function(value, index) {
if (iterator.call(context, value, index))
2007-01-18 22:24:27 +00:00
results.push(value);
});
return results;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function grep(filter, iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var results = [];
if (Object.isString(filter))
filter = new RegExp(RegExp.escape(filter));
2007-01-18 22:24:27 +00:00
this.each(function(value, index) {
if (filter.match(value))
results.push(iterator.call(context, value, index));
});
2007-01-18 22:24:27 +00:00
return results;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/** alias of: Enumerable#member
* Enumerable#include(object) -> Boolean
*
* Determines whether a given object is in the Enumerable or not,
* based on the `==` comparison operator. Aliased as [[Enumerable#member]].
**/
2008-12-11 10:33:31 +00:00
function include(object) {
if (Object.isFunction(this.indexOf))
if (this.indexOf(object) != -1) return true;
2007-01-18 22:24:27 +00:00
var found = false;
this.each(function(value) {
if (value == object) {
2007-01-18 22:24:27 +00:00
found = true;
throw $break;
}
});
return found;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#inGroupsOf(size[, filler = null]) -> [group...]
*
* Groups items in fixed-size chunks, using a specific value to fill up
* the last chunk if necessary.
**/
2008-12-11 10:33:31 +00:00
function inGroupsOf(number, fillWith) {
fillWith = Object.isUndefined(fillWith) ? null : fillWith;
2007-01-18 22:24:27 +00:00
return this.eachSlice(number, function(slice) {
while(slice.length < number) slice.push(fillWith);
return slice;
});
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function inject(memo, iterator, context) {
2007-01-18 22:24:27 +00:00
this.each(function(value, index) {
memo = iterator.call(context, memo, value, index);
2007-01-18 22:24:27 +00:00
});
return memo;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function invoke(method) {
2007-01-18 22:24:27 +00:00
var args = $A(arguments).slice(1);
return this.map(function(value) {
return value[method].apply(value, args);
});
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function max(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var result;
this.each(function(value, index) {
value = iterator.call(context, value, index);
if (result == null || value >= result)
2007-01-18 22:24:27 +00:00
result = value;
});
return result;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function min(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var result;
this.each(function(value, index) {
value = iterator.call(context, value, index);
if (result == null || value < result)
2007-01-18 22:24:27 +00:00
result = value;
});
return result;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function partition(iterator, context) {
iterator = iterator || Prototype.K;
2007-01-18 22:24:27 +00:00
var trues = [], falses = [];
this.each(function(value, index) {
(iterator.call(context, value, index) ?
2007-01-18 22:24:27 +00:00
trues : falses).push(value);
});
return [trues, falses];
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#pluck(propertyName) -> Array
*
* Optimization for a common use-case of collect: fetching the same property
* for all the elements. Returns the property values.
**/
2008-12-11 10:33:31 +00:00
function pluck(property) {
2007-01-18 22:24:27 +00:00
var results = [];
this.each(function(value) {
2007-01-18 22:24:27 +00:00
results.push(value[property]);
});
return results;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#reject(iterator[, context]) -> Array
*
* Returns all the elements for which the iterator returned a falsy value.
**/
2008-12-11 10:33:31 +00:00
function reject(iterator, context) {
2007-01-18 22:24:27 +00:00
var results = [];
this.each(function(value, index) {
if (!iterator.call(context, value, index))
2007-01-18 22:24:27 +00:00
results.push(value);
});
return results;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#sortBy(iterator[, context]) -> Array
*
* Provides a custom-sorted view of the elements based on the criteria computed,
* for each element, by the iterator.
**/
2008-12-11 10:33:31 +00:00
function sortBy(iterator, context) {
2007-01-18 22:24:27 +00:00
return this.map(function(value, index) {
return {
value: value,
criteria: iterator.call(context, value, index)
};
2007-01-18 22:24:27 +00:00
}).sort(function(left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
}).pluck('value');
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/** alias of: Enumerable#entries
* Enumerable#toArray() -> Array
*
* Returns an Array representation of the enumeration.
* Aliased as [[Enumerable#entries]].
**/
2008-12-11 10:33:31 +00:00
function toArray() {
2007-01-18 22:24:27 +00:00
return this.map();
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* 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.
**/
2008-12-11 10:33:31 +00:00
function zip() {
2007-01-18 22:24:27 +00:00
var iterator = Prototype.K, args = $A(arguments);
if (Object.isFunction(args.last()))
2007-01-18 22:24:27 +00:00
iterator = args.pop();
var collections = [this].concat(args).map($A);
return this.map(function(value, index) {
return iterator(collections.pluck(index));
});
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/**
* Enumerable#size() -> Number
*
* Returns the size of the enumeration.
**/
2008-12-11 10:33:31 +00:00
function size() {
2007-01-18 22:24:27 +00:00
return this.toArray().length;
2008-12-11 10:33:31 +00:00
}
2007-01-18 22:24:27 +00:00
2008-12-14 04:36:59 +00:00
/** related to: Object.inspect
* Enumerable#inspect() -> String
*
* TODO: Enumerable#inspect
**/
2008-12-11 10:33:31 +00:00
function inspect() {
2007-01-18 22:24:27 +00:00
return '#<Enumerable:' + this.toArray().inspect() + '>';
}
2008-12-11 10:33:31 +00:00
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
};
})();