wu range fix

by Kaleb Hornsby

HTML

<script src="http://fitzgen.github.com/wu.js/lib/wu.js"></script>

JavaScript

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());