prototype/src/enumerable.js

221 lines
5.5 KiB
JavaScript
Raw Normal View History

var $break = { };
2007-01-18 22:24:27 +00:00
var Enumerable = {
each: function(iterator, context) {
2007-01-18 22:24:27 +00:00
var index = 0;
iterator = iterator.bind(context);
2007-01-18 22:24:27 +00:00
try {
this._each(function(value) {
iterator(value, index++);
2007-01-18 22:24:27 +00:00
});
} catch (e) {
if (e != $break) throw e;
}
return this;
},
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));
return slices.collect(iterator, context);
2007-01-18 22:24:27 +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) {
result = result && !!iterator(value, index);
2007-01-18 22:24:27 +00:00
if (!result) throw $break;
});
return result;
},
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) {
if (result = !!iterator(value, index))
2007-01-18 22:24:27 +00:00
throw $break;
});
return result;
},
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) {
results.push(iterator(value, index));
2007-01-18 22:24:27 +00:00
});
return results;
},
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;
},
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;
},
grep: function(filter, iterator, context) {
iterator = iterator ? iterator.bind(context) : 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(value, index));
});
2007-01-18 22:24:27 +00:00
return results;
},
include: function(object) {
if (Object.isFunction(this.indexOf))
return this.indexOf(object) != -1;
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;
},
inGroupsOf: function(number, fillWith) {
fillWith = fillWith === undefined ? null : fillWith;
return this.eachSlice(number, function(slice) {
while(slice.length < number) slice.push(fillWith);
return slice;
});
},
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);
});
},
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) {
value = iterator(value, index);
2007-01-18 22:24:27 +00:00
if (result == undefined || value >= result)
result = value;
});
return result;
},
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) {
value = iterator(value, index);
2007-01-18 22:24:27 +00:00
if (result == undefined || value < result)
result = value;
});
return result;
},
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) {
(iterator(value, index) ?
2007-01-18 22:24:27 +00:00
trues : falses).push(value);
});
return [trues, falses];
},
pluck: function(property) {
var results = [];
this.each(function(value) {
2007-01-18 22:24:27 +00:00
results.push(value[property]);
});
return results;
},
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;
},
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);
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-01-18 22:24:27 +00:00
Object.extend(Enumerable, {
map: Enumerable.collect,
find: Enumerable.detect,
select: Enumerable.findAll,
filter: Enumerable.findAll,
2007-01-18 22:24:27 +00:00
member: Enumerable.include,
entries: Enumerable.toArray,
every: Enumerable.all,
some: Enumerable.any
2007-01-18 22:24:27 +00:00
});