mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
initial implementation of _.range
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user