var throwNewTypeError = function(msg) {
throw new TypeError("wu.js: " + msg);
}
, toObjProtoString = function (obj) {
return Object.prototype.toString.call(obj);
}
, OBJECT_NUMBER_STR = "[object Number]"
, rangeHelper = function(start, end, incr) {
// Ensure the types of our parameters are numbers (and not NaN or
// Infinity), because we could accidentally begin an infinite loop.
start = toObjProtoString(start) === OBJECT_NUMBER_STR && !isNaN(start) && start !== Infinity && start !== -Infinity ? start : throwNewTypeError("The `start` parameter to wu.range must be a number.");
incr = toObjProtoString(incr) === OBJECT_NUMBER_STR && !isNaN(incr) && incr !== Infinity && incr !== -Infinity ? incr : throwNewTypeError("The `incr` parameter to wu.range must be a number.");
// However, end can be infinite.
end = toObjProtoString(end) === OBJECT_NUMBER_STR && !isNaN(end) ? end : throwNewTypeError("The `end` parameter to wu.range must be a number.");
// Handle first case since we are doing +=
start = start - incr;
return wu.Iterator(function() {
return start + incr >= end ? this.stop() : start += incr;
});
};
wu.range = function() {
switch (arguments.length) {
case 1:
return rangeHelper(0, arguments[0], 1);
case 2:
return rangeHelper(arguments[0], arguments[1], 1);
case 3:
return rangeHelper(arguments[0], arguments[1], arguments[2]);
default:
throw new TypeError("wu.js: Wrong number of arguments passed to wu.range!");
}
};
console.log(wu.range(5).toArray());
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.