mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 20:37:48 +00:00
Ensure -0 is treated as 0.
This commit is contained in:
42
lodash.js
42
lodash.js
@@ -2417,7 +2417,7 @@
|
|||||||
}
|
}
|
||||||
outer:
|
outer:
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = array[index],
|
var value = (value = array[index]) === 0 ? 0 : value,
|
||||||
computed = iteratee ? iteratee(value) : value;
|
computed = iteratee ? iteratee(value) : value;
|
||||||
|
|
||||||
if (isCommon && computed === computed) {
|
if (isCommon && computed === computed) {
|
||||||
@@ -2775,7 +2775,7 @@
|
|||||||
|
|
||||||
outer:
|
outer:
|
||||||
while (++index < length && result.length < maxLength) {
|
while (++index < length && result.length < maxLength) {
|
||||||
var value = array[index],
|
var value = (value = array[index]) === 0 ? 0 : value,
|
||||||
computed = iteratee ? iteratee(value) : value;
|
computed = iteratee ? iteratee(value) : value;
|
||||||
|
|
||||||
if (!(seen
|
if (!(seen
|
||||||
@@ -3650,40 +3650,26 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.sortedUniq`.
|
* The base implementation of `_.sortedUniq` and `_.sortedUniqBy` without
|
||||||
*
|
* support for iteratee shorthands.
|
||||||
* @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.
|
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Array} array The array to inspect.
|
* @param {Array} array The array to inspect.
|
||||||
* @param {Function} [iteratee] The iteratee invoked per element.
|
* @param {Function} [iteratee] The iteratee invoked per element.
|
||||||
* @returns {Array} Returns the new duplicate free array.
|
* @returns {Array} Returns the new duplicate free array.
|
||||||
*/
|
*/
|
||||||
function baseSortedUniqBy(array, iteratee) {
|
function baseSortedUniq(array, iteratee) {
|
||||||
var index = 0,
|
var index = -1,
|
||||||
length = array.length,
|
length = array.length,
|
||||||
value = array[0],
|
resIndex = 0,
|
||||||
computed = iteratee ? iteratee(value) : value,
|
result = [];
|
||||||
seen = computed,
|
|
||||||
resIndex = 1,
|
|
||||||
result = [value];
|
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
value = array[index],
|
var value = (value = array[index]) === 0 ? 0 : value,
|
||||||
computed = iteratee ? iteratee(value) : value;
|
computed = iteratee ? iteratee(value) : value;
|
||||||
|
|
||||||
if (!eq(computed, seen)) {
|
if (!index || !eq(computed, seen)) {
|
||||||
seen = computed;
|
var seen = computed;
|
||||||
result[resIndex++] = value;
|
result[resIndex++] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3763,7 +3749,7 @@
|
|||||||
}
|
}
|
||||||
outer:
|
outer:
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = array[index],
|
var value = (value = array[index]) === 0 ? 0 : value,
|
||||||
computed = iteratee ? iteratee(value) : value;
|
computed = iteratee ? iteratee(value) : value;
|
||||||
|
|
||||||
if (isCommon && computed === computed) {
|
if (isCommon && computed === computed) {
|
||||||
@@ -7267,7 +7253,7 @@
|
|||||||
*/
|
*/
|
||||||
function sortedUniqBy(array, iteratee) {
|
function sortedUniqBy(array, iteratee) {
|
||||||
return (array && array.length)
|
return (array && array.length)
|
||||||
? baseSortedUniqBy(array, getIteratee(iteratee))
|
? baseSortedUniq(array, getIteratee(iteratee))
|
||||||
: [];
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -4722,7 +4722,7 @@
|
|||||||
assert.deepEqual(actual, [1, 3]);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var values = [-0, 0],
|
var values = [-0, 0],
|
||||||
@@ -4756,7 +4756,7 @@
|
|||||||
assert.deepEqual(func(array1, array2), [LARGE_ARRAY_SIZE]);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var values = [-0, 0],
|
var values = [-0, 0],
|
||||||
@@ -7971,11 +7971,11 @@
|
|||||||
assert.deepEqual(func(args, array), expected);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var values = [-0, 0],
|
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 actual = lodashStable.map(values, function(value) {
|
||||||
return lodashStable.map(func(values, [value]), lodashStable.toString);
|
return lodashStable.map(func(values, [value]), lodashStable.toString);
|
||||||
@@ -7991,11 +7991,11 @@
|
|||||||
assert.deepEqual(actual, [NaN]);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var values = [-0, 0],
|
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 actual = lodashStable.map(values, function(value) {
|
||||||
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(value));
|
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, lodashStable.constant(value));
|
||||||
@@ -24113,11 +24113,11 @@
|
|||||||
assert.deepEqual(func(objects), objects);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var actual = lodashStable.map(func([-0, 0]), lodashStable.toString);
|
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) {
|
QUnit.test('`_.' + methodName + '` should match `NaN`', function(assert) {
|
||||||
@@ -24142,7 +24142,7 @@
|
|||||||
assert.deepEqual(func(largeArray), expected);
|
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);
|
assert.expect(1);
|
||||||
|
|
||||||
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
|
||||||
|
|||||||
Reference in New Issue
Block a user