initial implementation of _.range

This commit is contained in:
Kirill Ishanov
2009-12-01 00:44:13 +03:00
parent 67f1e8a9c8
commit d8cf99ba89
2 changed files with 18 additions and 0 deletions

View File

@@ -2,6 +2,10 @@ $(document).ready(function() {
module("Collection functions (each, any, select, and so on...)");
test("generators: range", function() {
equals(_.range(4).join(' '), '0 1 2 3 4', 'range with positive number generates an array of of elements 0,1,2,...,n');
});
test("collections: each", function() {
_.each([1, 2, 3], function(num, i) {
equals(num, i + 1, 'each iterators provide value and iteration count');

View File

@@ -33,6 +33,20 @@
// Current version.
_.VERSION = '0.4.5';
/*------------------------ Generator Functions: ----------------------------*/
_.range = function(upper, lower, step) {
if (!lower) var lower = 0;
if (!step) var step = 1;
var result = new Array(((upper - lower) / step) + 1));
for (var i = lower; i <= upper; i += step) {
result[i] = i;
}
return result;
}
/*------------------------ Collection Functions: ---------------------------*/
// The cornerstone, an each implementation.