Only avoid converting -0 to 0 if comparator is provided.

This commit is contained in:
John-David Dalton
2016-04-20 12:33:26 -07:00
parent 0e4b307585
commit 6987d70dc2
2 changed files with 72 additions and 17 deletions

View File

@@ -2417,9 +2417,10 @@
} }
outer: outer:
while (++index < length) { while (++index < length) {
var value = (value = array[index]) === 0 ? 0 : value, var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee ? iteratee(value) : value;
value = (comparator || value !== 0) ? value : 0;
if (isCommon && computed === computed) { if (isCommon && computed === computed) {
var valuesIndex = valuesLength; var valuesIndex = valuesLength;
while (valuesIndex--) { while (valuesIndex--) {
@@ -2775,9 +2776,10 @@
outer: outer:
while (++index < length && result.length < maxLength) { while (++index < length && result.length < maxLength) {
var value = (value = array[index]) === 0 ? 0 : value, var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee ? iteratee(value) : value;
value = (comparator || value !== 0) ? value : 0;
if (!(seen if (!(seen
? cacheHas(seen, computed) ? cacheHas(seen, computed)
: includes(result, computed, comparator) : includes(result, computed, comparator)
@@ -3665,12 +3667,12 @@
result = []; result = [];
while (++index < length) { while (++index < length) {
var value = (value = array[index]) === 0 ? 0 : value, var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee ? iteratee(value) : value;
if (!index || !eq(computed, seen)) { if (!index || !eq(computed, seen)) {
var seen = computed; var seen = computed;
result[resIndex++] = value; result[resIndex++] = value === 0 ? 0 : value;
} }
} }
return result; return result;
@@ -3749,9 +3751,10 @@
} }
outer: outer:
while (++index < length) { while (++index < length) {
var value = (value = array[index]) === 0 ? 0 : value, var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee ? iteratee(value) : value;
value = (comparator || value !== 0) ? value : 0;
if (isCommon && computed === computed) { if (isCommon && computed === computed) {
var seenIndex = seen.length; var seenIndex = seen.length;
while (seenIndex--) { while (seenIndex--) {

View File

@@ -4723,16 +4723,18 @@
}); });
QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) { QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) {
assert.expect(1); assert.expect(2);
var values = [-0, 0], var array = [-0, 0];
expected = lodashStable.map(values, alwaysEmptyArray);
var actual = lodashStable.map(values, function(value) { var actual = lodashStable.map(array, function(value) {
return func(values, [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) { 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) { QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` as `0`', function(assert) {
assert.expect(1); assert.expect(2);
var values = [-0, 0], var array = [-0, 0];
expected = lodashStable.map(values, alwaysEmptyArray);
var actual = lodashStable.map(values, function(value) { var actual = lodashStable.map(array, function(value) {
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(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) { QUnit.test('`_.' + methodName + '` should work with large arrays of `NaN`', function(assert) {
@@ -4839,6 +4844,21 @@
assert.deepEqual(actual, [objects[1]]); 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]]); 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]]); 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);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/