Attempt again to ensure again _.toInteger converts Infinity to an integer.

This commit is contained in:
John-David Dalton
2015-09-15 23:41:53 -07:00
parent eb57e522aa
commit 00d26a6419
2 changed files with 14 additions and 13 deletions

View File

@@ -51,7 +51,7 @@
/** Used as references for various `Number` constants. */ /** Used as references for various `Number` constants. */
var INFINITY = 1 / 0, var INFINITY = 1 / 0,
MAX_SAFE_INTEGER = 9007199254740991, MAX_SAFE_INTEGER = 9007199254740991,
MAX_VALUE = 1.7976931348623157e308; MAX_INTEGER = 1e308;
/** Used as references for the maximum length and index of an array. */ /** Used as references for the maximum length and index of an array. */
var MAX_ARRAY_LENGTH = 4294967295, var MAX_ARRAY_LENGTH = 4294967295,
@@ -9072,13 +9072,12 @@
* // => 0 * // => 0
* *
* _.toInteger(-Infinity) * _.toInteger(-Infinity)
* // => -Number.MAX_VALUE * // => -1e308
*/ */
function toInteger(value) { function toInteger(value) {
var result = nativeFloor(value) || 0; return (value == INFINITY || value == -INFINITY)
return result == INFINITY ? (value < 0 ? -1 : 1) * MAX_INTEGER
? MAX_VALUE : (value - (value % 1)) || 0;
: (result == -INFINITY ? -MAX_VALUE : result);
} }
/** /**

View File

@@ -12,12 +12,13 @@
/** Used as the `TypeError` message for "Functions" methods. */ /** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function'; var FUNC_ERROR_TEXT = 'Expected a function';
/** Used as references for the max length and index of an array. */ /** Used as references for various `Number` constants. */
var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1, var MAX_SAFE_INTEGER = 9007199254740991,
MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1; MAX_INTEGER = 1e308;
/** Used as the maximum length an array-like object. */ /** Used as references for the maximum length and index of an array. */
var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; var MAX_ARRAY_LENGTH = 4294967295,
MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
/** `Object#toString` result references. */ /** `Object#toString` result references. */
var funcTag = '[object Function]', var funcTag = '[object Function]',
@@ -18430,12 +18431,13 @@
(function() { (function() {
QUnit.test('should convert values to integers', function(assert) { QUnit.test('should convert values to integers', function(assert) {
assert.expect(4); assert.expect(5);
assert.strictEqual(_.toInteger('3.14'), 3); assert.strictEqual(_.toInteger('3.14'), 3);
assert.strictEqual(_.toInteger(), 0); assert.strictEqual(_.toInteger(), 0);
assert.strictEqual(_.toInteger(NaN), 0); assert.strictEqual(_.toInteger(NaN), 0);
assert.strictEqual(_.toInteger(-Infinity), -1.7976931348623157e+308); assert.strictEqual(_.toInteger(Infinity), MAX_INTEGER);
assert.strictEqual(_.toInteger(-Infinity), -MAX_INTEGER);
}); });
}()); }());