From bc2adb208c6fd70516b68a6682aece81271f8a46 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 28 Oct 2015 07:29:20 -0700 Subject: [PATCH] Ensure `_.toString` produces the correct result for `Object(-0)`. --- lodash.js | 7 ++----- test/test.js | 12 +++++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lodash.js b/lodash.js index 581072eb4..63955e1ca 100644 --- a/lodash.js +++ b/lodash.js @@ -9760,15 +9760,12 @@ * @returns {string} Returns the string. */ function toString(value) { - // Preserve sign of `0`. - if (value === 0) { - return (1 / value) == INFINITY ? '0' : '-0'; - } // Exit early for strings to avoid a performance hit in some environments. if (typeof value == 'string') { return value; } - return value == null ? '' : (value + ''); + var result = value == null ? '' : (value + ''); + return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; } /*------------------------------------------------------------------------*/ diff --git a/test/test.js b/test/test.js index bd43b531f..9b6863200 100644 --- a/test/test.js +++ b/test/test.js @@ -19440,10 +19440,16 @@ }); QUnit.test('should preserve sign of `0`', function(assert) { - assert.expect(2); + assert.expect(1); - assert.strictEqual(_.toString(0), '0'); - assert.strictEqual(_.toString(-0), '-0'); + var values = [0, Object(0), -0, Object(-0)], + expected = ['0', '0', '-0', '-0']; + + var actual = lodashStable.map(values, function(value) { + return _.toString(value); + }); + + assert.deepEqual(actual, expected); }); QUnit.test('should return the `toString` result of the wrapped value', function(assert) {