Ensure -0 is treated as 0.

This commit is contained in:
John-David Dalton
2016-04-20 00:51:25 -07:00
parent 80f35ee713
commit 0429434dbb
2 changed files with 23 additions and 37 deletions

View File

@@ -2417,7 +2417,7 @@
}
outer:
while (++index < length) {
var value = array[index],
var value = (value = array[index]) === 0 ? 0 : value,
computed = iteratee ? iteratee(value) : value;
if (isCommon && computed === computed) {
@@ -2775,7 +2775,7 @@
outer:
while (++index < length && result.length < maxLength) {
var value = array[index],
var value = (value = array[index]) === 0 ? 0 : value,
computed = iteratee ? iteratee(value) : value;
if (!(seen
@@ -3650,40 +3650,26 @@
}
/**
* The base implementation of `_.sortedUniq`.
*
* @private
* @param {Array} array The array to inspect.
* @returns {Array} Returns the new duplicate free array.
*/
function baseSortedUniq(array) {
return baseSortedUniqBy(array);
}
/**
* The base implementation of `_.sortedUniqBy` without support for iteratee
* shorthands.
* The base implementation of `_.sortedUniq` and `_.sortedUniqBy` without
* support for iteratee shorthands.
*
* @private
* @param {Array} array The array to inspect.
* @param {Function} [iteratee] The iteratee invoked per element.
* @returns {Array} Returns the new duplicate free array.
*/
function baseSortedUniqBy(array, iteratee) {
var index = 0,
function baseSortedUniq(array, iteratee) {
var index = -1,
length = array.length,
value = array[0],
computed = iteratee ? iteratee(value) : value,
seen = computed,
resIndex = 1,
result = [value];
resIndex = 0,
result = [];
while (++index < length) {
value = array[index],
computed = iteratee ? iteratee(value) : value;
var value = (value = array[index]) === 0 ? 0 : value,
computed = iteratee ? iteratee(value) : value;
if (!eq(computed, seen)) {
seen = computed;
if (!index || !eq(computed, seen)) {
var seen = computed;
result[resIndex++] = value;
}
}
@@ -3763,7 +3749,7 @@
}
outer:
while (++index < length) {
var value = array[index],
var value = (value = array[index]) === 0 ? 0 : value,
computed = iteratee ? iteratee(value) : value;
if (isCommon && computed === computed) {
@@ -7267,7 +7253,7 @@
*/
function sortedUniqBy(array, iteratee) {
return (array && array.length)
? baseSortedUniqBy(array, getIteratee(iteratee))
? baseSortedUniq(array, getIteratee(iteratee))
: [];
}

View File

@@ -4722,7 +4722,7 @@
assert.deepEqual(actual, [1, 3]);
});
QUnit.test('`_.' + methodName + '` should treat `-0` and `0` as the same value', function(assert) {
QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) {
assert.expect(1);
var values = [-0, 0],
@@ -4756,7 +4756,7 @@
assert.deepEqual(func(array1, array2), [LARGE_ARRAY_SIZE]);
});
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` and `0`', function(assert) {
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` as `0`', function(assert) {
assert.expect(1);
var values = [-0, 0],
@@ -7971,11 +7971,11 @@
assert.deepEqual(func(args, array), expected);
});
QUnit.test('`_.' + methodName + '` should treat `-0` and `0` as the same value', function(assert) {
QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) {
assert.expect(1);
var values = [-0, 0],
expected = lodashStable.map(values, lodashStable.constant(['-0']));
expected = lodashStable.map(values, lodashStable.constant(['0']));
var actual = lodashStable.map(values, function(value) {
return lodashStable.map(func(values, [value]), lodashStable.toString);
@@ -7991,11 +7991,11 @@
assert.deepEqual(actual, [NaN]);
});
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` and `0`', function(assert) {
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` as `0`', function(assert) {
assert.expect(1);
var values = [-0, 0],
expected = lodashStable.map(values, lodashStable.constant(['-0']));
expected = lodashStable.map(values, lodashStable.constant(['0']));
var actual = lodashStable.map(values, function(value) {
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(value));
@@ -24113,11 +24113,11 @@
assert.deepEqual(func(objects), objects);
});
QUnit.test('`_.' + methodName + '` should treat `-0` and `0` as the same value', function(assert) {
QUnit.test('`_.' + methodName + '` should treat `-0` as `0`', function(assert) {
assert.expect(1);
var actual = lodashStable.map(func([-0, 0]), lodashStable.toString);
assert.deepEqual(actual, ['-0']);
assert.deepEqual(actual, ['0']);
});
QUnit.test('`_.' + methodName + '` should match `NaN`', function(assert) {
@@ -24142,7 +24142,7 @@
assert.deepEqual(func(largeArray), expected);
});
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` and `0`', function(assert) {
QUnit.test('`_.' + methodName + '` should work with large arrays of `-0` as `0`', function(assert) {
assert.expect(1);
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, function(index) {