prototype/src/lang/enumerable.js

240 lines
5.7 KiB
JavaScript
Raw Normal View History

var $break = { };
2007-01-18 22:24:27 +00:00
2008-12-11 10:33:31 +00:00
var Enumerable = (function() {
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-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-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-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-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-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-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-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(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-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-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-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-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-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-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-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-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-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-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-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-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-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-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
};
})();