Ensure _.toInteger can return -0.

This commit is contained in:
John-David Dalton
2015-10-21 22:04:14 -07:00
parent 1513e46605
commit 5ec6f1085a
2 changed files with 12 additions and 3 deletions

View File

@@ -9736,9 +9736,12 @@
* // => -1e308
*/
function toInteger(value) {
return (value == INFINITY || value == -INFINITY)
? (value < 0 ? -1 : 1) * MAX_INTEGER
: (value - (value % 1)) || 0;
if (value === INFINITY || value === -INFINITY) {
return (value < 0 ? -1 : 1) * MAX_INTEGER;
}
value = +value;
var remainder = value % 1;
return value === value ? (remainder ? value - remainder : value) : 0;
}
/**

View File

@@ -19063,6 +19063,12 @@
assert.strictEqual(_.toInteger(Infinity), MAX_INTEGER);
assert.strictEqual(_.toInteger(-Infinity), -MAX_INTEGER);
});
QUnit.test('should not coerce `-0` to `0`', function(assert) {
assert.expect(1);
assert.strictEqual(1 / _.toInteger(-0), -Infinity);
});
}());
/*--------------------------------------------------------------------------*/