mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 20:37:48 +00:00
Only avoid converting -0 to 0 if comparator is provided.
This commit is contained in:
13
lodash.js
13
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--) {
|
||||
|
||||
76
test/test.js
76
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);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user