var $break = { };
var Enumerable = (function() {
//Transfer each data
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}
//Divide the data into N groups, where each group has a number of numbers, and the last group may be less than the number of numbers
function eachSlice(number, iterator, context) {
var index = -number, slices = [], array = ();
if (number < 1) return array;
while ((index += number) < )
((index, index+number));
return (iterator, context);
}
//Test whether all data meets a certain condition
function all(iterator, context) {
iterator = iterator || ;
var result = true;
(function(value, index) {
result = result && !!(context, value, index);
if (!result) throw $break;
});
return result;
}
//Check whether any data meets a certain condition
function any(iterator, context) {
iterator = iterator || ;
var result = false;
(function(value, index) {
if (result = !!(context, value, index))
throw $break;
});
return result;
}
//Any operation can be performed on all data and the result array can be returned
function collect(iterator, context) {
iterator = iterator || ;
var results = [];
(function(value, index) {
((context, value, index));
});
return results;
}
// Find the first data that meets a certain condition and return an alias equivalent to the find method
function detect(iterator, context) {
var result;
(function(value, index) {
if ((context, value, index)) {
result = value;
throw $break;
}
});
return result;
}
//Find all data that meets a certain condition and return the result
function findAll(iterator, context) {
var results = [];
(function(value, index) {
if ((context, value, index))
(value);
});
return results;
}
//Filter all data according to filter conditions, find data that meets filter conditions, and return the result
//filter is a string or regular expression
function grep(filter, iterator, context) {
iterator = iterator || ;
var results = [];
if ((filter))
filter = new RegExp((filter));
(function(value, index) {
if ((value))
((context, value, index));
});
return results;
}
//Check whether it contains some data
function include(object) {
if (())
if ((object) != -1) return true;
var found = false;
(function(value) {
if (value == object) {
found = true;
throw $break;
}
});
return found;
}
//Similar to eachSlice method, if the last group of elements is less than number, fill with fillWith parameter
function inGroupsOf(number, fillWith) {
fillWith = (fillWith) ? null : fillWith;
return (number, function(slice) {
while( < number) (fillWith);
return slice;
});
}
//Accumulate or multiply all data continuously, and other operations can be implemented such as accumulation or multiplication.
function inject(memo, iterator, context) {
(function(value, index) {
memo = (context, memo, value, index);
});
return memo;
}
//Execute a method on all data
function invoke(method) {
var args = $A(arguments).slice(1);
return (function(value) {
return value[method].apply(value, args);
});
}
// Find the maximum value in the data
function max(iterator, context) {
iterator = iterator || ;
var result;
(function(value, index) {
value = (context, value, index);
if (result == null || value >= result)
result = value;
});
return result;
}
//Find the minimum value in the data
function min(iterator, context) {
iterator = iterator || ;
var result;
(function(value, index) {
value = (context, value, index);
if (result == null || value < result)
result = value;
});
return result;
}
//Divide all data into two. The first group is data that meets a certain condition, and the second group is data that does not meet the condition.
function partition(iterator, context) {
iterator = iterator || ;
var trues = [], falses = [];
(function(value, index) {
((context, value, index) ?
trues : falses).push(value);
});
return [trues, falses];
}
//Fetch out the property value of all data and return the result
function pluck(property) {
var results = [];
(function(value) {
(value[property]);
});
return results;
}
//Find data that does not meet a certain condition
function reject(iterator, context) {
var results = [];
(function(value, index) {
if (!(context, value, index))
(value);
});
return results;
}
//Sort all data according to a certain condition
function sortBy(iterator, context) {
return (function(value, index) {
return {
value: value,
criteria: (context, value, index)
};
}).sort(function(left, right) {
var a = , b = ;
return a < b ? -1 : a > b ? 1 : 0;
}).pluck('value');
}
//Return the array representation of data
function toArray() {
return ();
}
// Basically, put two sets of data together for certain operations
function zip() {
var iterator = , args = $A(arguments);
if ((()))
iterator = ();
var collections = [this].concat(args).map($A);
return (function(value, index) {
return iterator((index));
});
}
function size() {
return ().length;
}
//Return the string representation of the Enumerable object
function inspect() {
return '#<Enumerable:' + ().inspect() + '>';
}
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
};
})();