improve arguments overloading in _.range method

This commit is contained in:
lifesinger
2011-01-18 10:01:36 +08:00
parent 840f19c102
commit 6c2693a5ea

View File

@@ -383,19 +383,21 @@
// the native Python `range()` function. See // the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range). // [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) { _.range = function(start, stop, step) {
var args = slice.call(arguments); if (arguments.length <= 1) {
var solo = args.length <= 1; stop = start || 0;
start = solo ? 0 : args[0]; start = 0;
stop = solo ? args[0] : args[1]; }
step = args[2] || 1; step = arguments[2] || 1;
var range = [];
var len = Math.max(Math.ceil((stop - start) / step), 0); var len = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0; var idx = 0;
var range = new Array(len);
while (idx < len) { while(idx < len) {
range[idx++] = start; range[idx++] = start;
start += step; start += step;
} }
return range; return range;
}; };