diff --git a/test/collections.js b/test/collections.js index 0f3381b9d..c0bfe1ee8 100644 --- a/test/collections.js +++ b/test/collections.js @@ -2,10 +2,6 @@ $(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'); diff --git a/test/generators.js b/test/generators.js new file mode 100644 index 000000000..ea7982de1 --- /dev/null +++ b/test/generators.js @@ -0,0 +1,15 @@ +$(document).ready(function() { + + module("Generator functions (range...)"); + + test("generators: range", function() { + equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array'); + equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1'); + equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a b-a, a < b generates an array with a single element, equal to a'); + equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b'); + }); +}); + diff --git a/test/test.html b/test/test.html index ea26345fb..86b73318e 100644 --- a/test/test.html +++ b/test/test.html @@ -7,6 +7,7 @@ + @@ -31,4 +32,4 @@
- \ No newline at end of file + diff --git a/test/utility.js b/test/utility.js index 6fd19af93..9f6c855e9 100644 --- a/test/utility.js +++ b/test/utility.js @@ -36,7 +36,7 @@ $(document).ready(function() { "flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include", "indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual", "isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", - "methods", "min", "pluck", "reduce", "reduceRight", "reject", "rest", "select", + "methods", "min", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", "size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq", "uniqueId", "values", "without", "wrap", "zip"]; ok(_(expected).isEqual(_.methods()), 'provides a sorted list of functions'); diff --git a/underscore.js b/underscore.js index 9d71638b5..dacba7c01 100644 --- a/underscore.js +++ b/underscore.js @@ -33,19 +33,33 @@ // Current version. _.VERSION = '0.4.5'; - /*------------------------ Generator Functions: ----------------------------*/ - _.range = function(upper, lower, step) { - if (!lower) var lower = 0; - if (!step) var step = 1; + /*------------------------ Generator Functions: ---------------------------*/ - var result = new Array(((upper - lower) / step) + 1)); - - for (var i = lower; i <= upper; i += step) { - result[i] = i; + // Generates an Array, containing an arithmetic progressions + // Analog of python's built-in function 'range' + _.range = function(start, stop, step) { + if (!stop) { + var stop = start; + start = 0; } - return result; - } + if (!step) var step = 1; + + var length = Math.ceil((stop - start) / step); + + if (length < 0) { + return []; + } + + var results = new Array(length); + var resIdx = 0; + + for (var i = start; (start <= stop ? stop - i > 0 : i - stop > 0); i += step) { + results[resIdx++] = i; + } + + return results; + }; /*------------------------ Collection Functions: ---------------------------*/