2007-08-08 23:19:45 +00:00
|
|
|
var $break = { };
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
var Enumerable = {
|
2007-07-09 18:55:58 +00:00
|
|
|
each: function(iterator, context) {
|
2007-01-18 22:24:27 +00:00
|
|
|
var index = 0;
|
2007-07-09 18:55:58 +00:00
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
try {
|
|
|
|
this._each(function(value) {
|
2007-01-18 22:59:23 +00:00
|
|
|
iterator(value, index++);
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e != $break) throw e;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
eachSlice: function(number, iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var index = -number, slices = [], array = this.toArray();
|
|
|
|
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);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
all: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = true;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
result = result && !!iterator(value, index);
|
2007-01-18 22:24:27 +00:00
|
|
|
if (!result) throw $break;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
any: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = false;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
if (result = !!iterator(value, index))
|
2007-01-18 22:24:27 +00:00
|
|
|
throw $break;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
collect: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
results.push(iterator(value, index));
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
detect: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (iterator(value, index)) {
|
|
|
|
result = value;
|
|
|
|
throw $break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
findAll: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (iterator(value, index))
|
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-24 16:47:12 +00:00
|
|
|
grep: function(filter, iterator, context) {
|
2007-07-09 18:55:58 +00:00
|
|
|
iterator = iterator ? iterator.bind(context) : 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))
|
2007-07-24 16:47:12 +00:00
|
|
|
filter = new RegExp(filter);
|
|
|
|
|
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))
|
2007-07-09 18:55:58 +00:00
|
|
|
results.push(iterator(value, index));
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
include: function(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;
|
|
|
|
},
|
|
|
|
|
|
|
|
inGroupsOf: function(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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
inject: function(memo, iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
this.each(function(value, index) {
|
|
|
|
memo = iterator(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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
max: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
value = iterator(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;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
min: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
value = iterator(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;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
partition: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var trues = [], falses = [];
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
(iterator(value, index) ?
|
2007-01-18 22:24:27 +00:00
|
|
|
trues : falses).push(value);
|
|
|
|
});
|
|
|
|
return [trues, falses];
|
|
|
|
},
|
|
|
|
|
|
|
|
pluck: function(property) {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
reject: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (!iterator(value, index))
|
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
sortBy: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.map(function(value, index) {
|
|
|
|
return {value: value, criteria: iterator(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);
|
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));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
size: function() {
|
|
|
|
return this.toArray().length;
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return '#<Enumerable:' + this.toArray().inspect() + '>';
|
|
|
|
}
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
Object.extend(Enumerable, {
|
|
|
|
map: Enumerable.collect,
|
|
|
|
find: Enumerable.detect,
|
|
|
|
select: Enumerable.findAll,
|
2007-07-09 18:55:58 +00:00
|
|
|
filter: Enumerable.findAll,
|
2007-01-18 22:24:27 +00:00
|
|
|
member: Enumerable.include,
|
2007-07-09 18:55:58 +00:00
|
|
|
entries: Enumerable.toArray,
|
|
|
|
every: Enumerable.all,
|
|
|
|
some: Enumerable.any
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|