2007-08-08 23:19:45 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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();
|
2008-02-03 21:08:13 +00:00
|
|
|
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));
|
2007-07-09 18:55:58 +00:00
|
|
|
return slices.collect(iterator, context);
|
2008-12-11 10:33:31 +00:00
|
|
|
}
|
2007-07-09 18:55:58 +00:00
|
|
|
|
2008-12-11 10:33:31 +00:00
|
|
|
function all(iterator, context) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = true;
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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
|
|
|
}
|
2007-07-09 18:55:58 +00:00
|
|
|
|
2008-12-11 10:33:31 +00:00
|
|
|
function any(iterator, context) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = false;
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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
|
|
|
}
|
2007-07-09 18:55:58 +00:00
|
|
|
|
2008-12-11 10:33:31 +00:00
|
|
|
function collect(iterator, context) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
2007-07-24 16:47:12 +00:00
|
|
|
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isString(filter))
|
2008-12-20 00:50:08 +00:00
|
|
|
filter = new RegExp(RegExp.escape(filter));
|
2007-07-24 16:47:12 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
this.each(function(value, index) {
|
2007-07-24 16:47:12 +00:00
|
|
|
if (filter.match(value))
|
2008-03-09 06:56:03 +00:00
|
|
|
results.push(iterator.call(context, value, index));
|
2007-07-09 18:55:58 +00:00
|
|
|
});
|
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) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(this.indexOf))
|
2007-08-17 11:06:07 +00:00
|
|
|
if (this.indexOf(object) != -1) return true;
|
2007-07-09 18:55:58 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
var found = false;
|
|
|
|
this.each(function(value) {
|
2007-08-17 11:06:07 +00:00
|
|
|
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) {
|
2007-11-15 21:53:15 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
value = iterator.call(context, value, index);
|
2007-11-15 21:53:15 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
value = iterator.call(context, value, index);
|
2007-11-15 21:53:15 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
iterator = iterator || Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var trues = [], falses = [];
|
|
|
|
this.each(function(value, index) {
|
2008-03-09 06:56:03 +00:00
|
|
|
(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 = [];
|
2007-07-09 18:55:58 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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) {
|
2008-03-09 06:56:03 +00:00
|
|
|
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);
|
2007-08-08 23:19:45 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
})();
|