218 lines
5.3 KiB
JavaScript
218 lines
5.3 KiB
JavaScript
var $break = { };
|
|
|
|
var Enumerable = {
|
|
each: function(iterator, context) {
|
|
var index = 0;
|
|
try {
|
|
this._each(function(value) {
|
|
iterator.call(context, value, index++);
|
|
});
|
|
} catch (e) {
|
|
if (e != $break) throw e;
|
|
}
|
|
return this;
|
|
},
|
|
|
|
eachSlice: function(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);
|
|
},
|
|
|
|
all: function(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;
|
|
},
|
|
|
|
any: function(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;
|
|
},
|
|
|
|
collect: function(iterator, context) {
|
|
iterator = iterator || Prototype.K;
|
|
var results = [];
|
|
this.each(function(value, index) {
|
|
results.push(iterator.call(context, value, index));
|
|
});
|
|
return results;
|
|
},
|
|
|
|
detect: function(iterator, context) {
|
|
var result;
|
|
this.each(function(value, index) {
|
|
if (iterator.call(context, value, index)) {
|
|
result = value;
|
|
throw $break;
|
|
}
|
|
});
|
|
return result;
|
|
},
|
|
|
|
findAll: function(iterator, context) {
|
|
var results = [];
|
|
this.each(function(value, index) {
|
|
if (iterator.call(context, value, index))
|
|
results.push(value);
|
|
});
|
|
return results;
|
|
},
|
|
|
|
grep: function(filter, iterator, context) {
|
|
iterator = iterator || Prototype.K;
|
|
var results = [];
|
|
|
|
if (Object.isString(filter))
|
|
filter = new RegExp(filter);
|
|
|
|
this.each(function(value, index) {
|
|
if (filter.match(value))
|
|
results.push(iterator.call(context, value, index));
|
|
});
|
|
return results;
|
|
},
|
|
|
|
include: function(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;
|
|
},
|
|
|
|
inGroupsOf: function(number, fillWith) {
|
|
fillWith = Object.isUndefined(fillWith) ? null : fillWith;
|
|
return this.eachSlice(number, function(slice) {
|
|
while(slice.length < number) slice.push(fillWith);
|
|
return slice;
|
|
});
|
|
},
|
|
|
|
inject: function(memo, iterator, context) {
|
|
this.each(function(value, index) {
|
|
memo = iterator.call(context, memo, value, index);
|
|
});
|
|
return memo;
|
|
},
|
|
|
|
invoke: function(method) {
|
|
var args = $A(arguments).slice(1);
|
|
return this.map(function(value) {
|
|
return value[method].apply(value, args);
|
|
});
|
|
},
|
|
|
|
max: function(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;
|
|
},
|
|
|
|
min: function(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;
|
|
},
|
|
|
|
partition: function(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];
|
|
},
|
|
|
|
pluck: function(property) {
|
|
var results = [];
|
|
this.each(function(value) {
|
|
results.push(value[property]);
|
|
});
|
|
return results;
|
|
},
|
|
|
|
reject: function(iterator, context) {
|
|
var results = [];
|
|
this.each(function(value, index) {
|
|
if (!iterator.call(context, value, index))
|
|
results.push(value);
|
|
});
|
|
return results;
|
|
},
|
|
|
|
sortBy: function(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');
|
|
},
|
|
|
|
toArray: function() {
|
|
return this.map();
|
|
},
|
|
|
|
zip: function() {
|
|
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));
|
|
});
|
|
},
|
|
|
|
size: function() {
|
|
return this.toArray().length;
|
|
},
|
|
|
|
inspect: function() {
|
|
return '#<Enumerable:' + this.toArray().inspect() + '>';
|
|
}
|
|
};
|
|
|
|
Object.extend(Enumerable, {
|
|
map: Enumerable.collect,
|
|
find: Enumerable.detect,
|
|
select: Enumerable.findAll,
|
|
filter: Enumerable.findAll,
|
|
member: Enumerable.include,
|
|
entries: Enumerable.toArray,
|
|
every: Enumerable.all,
|
|
some: Enumerable.any
|
|
});
|