diff --git a/lodash.js b/lodash.js index 9992676b0..35b169c51 100644 --- a/lodash.js +++ b/lodash.js @@ -2417,9 +2417,10 @@ } outer: while (++index < length) { - var value = (value = array[index]) === 0 ? 0 : value, + var value = array[index], computed = iteratee ? iteratee(value) : value; + value = (comparator || value !== 0) ? value : 0; if (isCommon && computed === computed) { var valuesIndex = valuesLength; while (valuesIndex--) { @@ -2775,9 +2776,10 @@ outer: while (++index < length && result.length < maxLength) { - var value = (value = array[index]) === 0 ? 0 : value, + var value = array[index], computed = iteratee ? iteratee(value) : value; + value = (comparator || value !== 0) ? value : 0; if (!(seen ? cacheHas(seen, computed) : includes(result, computed, comparator) @@ -3665,12 +3667,12 @@ result = []; while (++index < length) { - var value = (value = array[index]) === 0 ? 0 : value, + var value = array[index], computed = iteratee ? iteratee(value) : value; if (!index || !eq(computed, seen)) { var seen = computed; - result[resIndex++] = value; + result[resIndex++] = value === 0 ? 0 : value; } } return result; @@ -3749,9 +3751,10 @@ } outer: while (++index < length) { - var value = (value = array[index]) === 0 ? 0 : value, + var value = array[index], computed = iteratee ? iteratee(value) : value; + value = (comparator || value !== 0) ? value : 0; if (isCommon && computed === computed) { var seenIndex = seen.length; while (seenIndex--) { diff --git a/test/test.js b/test/test.js index 6c6103a19..1e85f794a 100644 --- a/test/test.js +++ b/test/test.js @@ -4723,16 +4723,18 @@ }); QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) { - assert.expect(1); + assert.expect(2); - var values = [-0, 0], - expected = lodashStable.map(values, alwaysEmptyArray); + var array = [-0, 0]; - var actual = lodashStable.map(values, function(value) { - return func(values, [value]); + var actual = lodashStable.map(array, function(value) { + return func(array, [value]); }); - assert.deepEqual(actual, expected); + assert.deepEqual(actual, [[], []]); + + actual = lodashStable.map(func([-0, 1], [1]), lodashStable.toString); + assert.deepEqual(actual, ['0']); }); QUnit.test('`_.' + methodName + '` should match `NaN`', function(assert) { @@ -4757,17 +4759,20 @@ }); QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` as `0`', function(assert) { - assert.expect(1); + assert.expect(2); - var values = [-0, 0], - expected = lodashStable.map(values, alwaysEmptyArray); + var array = [-0, 0]; - var actual = lodashStable.map(values, function(value) { + var actual = lodashStable.map(array, function(value) { var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(value)); - return func(values, largeArray); + return func(array, largeArray); }); - assert.deepEqual(actual, expected); + assert.deepEqual(actual, [[], []]); + + var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, alwaysOne); + actual = lodashStable.map(func([-0, 1], largeArray), lodashStable.toString); + assert.deepEqual(actual, ['0']); }); QUnit.test('`_.' + methodName + '` should work with large arrays of `NaN`', function(assert) { @@ -4839,6 +4844,21 @@ assert.deepEqual(actual, [objects[1]]); }); + + QUnit.test('should preserve the sign of `0`', function(assert) { + assert.expect(1); + + var array = [-0, 1], + largeArray = lodashStable.times(LARGE_ARRAY_SIZE, alwaysOne), + others = [[1], largeArray], + expected = lodashStable.map(others, lodashStable.constant(['-0'])); + + var actual = lodashStable.map(others, function(other) { + return lodashStable.map(_.differenceWith(array, other, lodashStable.eq), lodashStable.toString); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -8087,6 +8107,21 @@ assert.deepEqual(actual, [objects[0]]); }); + + QUnit.test('should preserve the sign of `0`', function(assert) { + assert.expect(1); + + var array = [-0], + largeArray = lodashStable.times(LARGE_ARRAY_SIZE, alwaysZero), + others = [[0], largeArray], + expected = lodashStable.map(others, lodashStable.constant(['-0'])); + + var actual = lodashStable.map(others, function(other) { + return lodashStable.map(_.intersectionWith(array, other, lodashStable.eq), lodashStable.toString); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -24078,6 +24113,23 @@ assert.deepEqual(actual, [objects[0], objects[1]]); }); + + QUnit.test('should preserve the sign of `0`', function(assert) { + assert.expect(1); + + var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, function(index) { + return isEven(index) ? -0 : 0; + }); + + var arrays = [[-0, 0], largeArray], + expected = lodashStable.map(arrays, lodashStable.constant(['-0'])); + + var actual = lodashStable.map(arrays, function(array) { + return lodashStable.map(_.uniqWith(array, lodashStable.eq), lodashStable.toString); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/