Reorganize enumerable.js.

This commit is contained in:
Tobie Langel 2008-09-28 19:25:03 +02:00
parent 22f9cfc626
commit bcbdf3b247
1 changed files with 78 additions and 56 deletions

View File

@ -1,7 +1,7 @@
var $break = { };
var Enumerable = {
each: function(iterator, context) {
var Enumerable = (function() {
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
@ -11,17 +11,17 @@ var Enumerable = {
if (e != $break) throw e;
}
return this;
},
}
eachSlice: function(number, iterator, context) {
function eachSlice(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) {
function all(iterator, context) {
iterator = iterator || Prototype.K;
var result = true;
this.each(function(value, index) {
@ -29,9 +29,9 @@ var Enumerable = {
if (!result) throw $break;
});
return result;
},
}
any: function(iterator, context) {
function any(iterator, context) {
iterator = iterator || Prototype.K;
var result = false;
this.each(function(value, index) {
@ -39,18 +39,18 @@ var Enumerable = {
throw $break;
});
return result;
},
}
collect: function(iterator, context) {
function collect(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) {
function detect(iterator, context) {
var result;
this.each(function(value, index) {
if (iterator.call(context, value, index)) {
@ -59,18 +59,18 @@ var Enumerable = {
}
});
return result;
},
}
findAll: function(iterator, context) {
function findAll(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) {
function grep(filter, iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
@ -82,9 +82,9 @@ var Enumerable = {
results.push(iterator.call(context, value, index));
});
return results;
},
}
include: function(object) {
function include(object) {
if (Object.isFunction(this.indexOf))
if (this.indexOf(object) != -1) return true;
@ -96,31 +96,31 @@ var Enumerable = {
}
});
return found;
},
}
inGroupsOf: function(number, fillWith) {
function inGroupsOf(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) {
function inject(memo, iterator, context) {
this.each(function(value, index) {
memo = iterator.call(context, memo, value, index);
});
return memo;
},
}
invoke: function(method) {
function invoke(method) {
var args = $A(arguments).slice(1);
return this.map(function(value) {
return value[method].apply(value, args);
});
},
}
max: function(iterator, context) {
function max(iterator, context) {
iterator = iterator || Prototype.K;
var result;
this.each(function(value, index) {
@ -129,9 +129,9 @@ var Enumerable = {
result = value;
});
return result;
},
}
min: function(iterator, context) {
function min(iterator, context) {
iterator = iterator || Prototype.K;
var result;
this.each(function(value, index) {
@ -140,9 +140,9 @@ var Enumerable = {
result = value;
});
return result;
},
}
partition: function(iterator, context) {
function partition(iterator, context) {
iterator = iterator || Prototype.K;
var trues = [], falses = [];
this.each(function(value, index) {
@ -150,26 +150,26 @@ var Enumerable = {
trues : falses).push(value);
});
return [trues, falses];
},
}
pluck: function(property) {
function pluck(property) {
var results = [];
this.each(function(value) {
results.push(value[property]);
});
return results;
},
}
reject: function(iterator, context) {
function reject(iterator, context) {
var results = [];
this.each(function(value, index) {
if (!iterator.call(context, value, index))
results.push(value);
});
return results;
},
}
sortBy: function(iterator, context) {
function sortBy(iterator, context) {
return this.map(function(value, index) {
return {
value: value,
@ -179,13 +179,13 @@ var Enumerable = {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
}).pluck('value');
},
}
toArray: function() {
function toArray() {
return this.map();
},
}
zip: function() {
function zip() {
var iterator = Prototype.K, args = $A(arguments);
if (Object.isFunction(args.last()))
iterator = args.pop();
@ -194,24 +194,46 @@ var Enumerable = {
return this.map(function(value, index) {
return iterator(collections.pluck(index));
});
},
}
size: function() {
function size() {
return this.toArray().length;
},
}
inspect: function() {
function inspect() {
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
});
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
};
})();