mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 08:07:50 +00:00
Make _.add, _.subtract, and _.sum not skip NaN values.
This commit is contained in:
10
lodash.js
10
lodash.js
@@ -835,7 +835,7 @@
|
||||
|
||||
while (++index < length) {
|
||||
var current = iteratee(array[index]);
|
||||
if (current === current && current != null) {
|
||||
if (current !== undefined) {
|
||||
result = result === undefined ? current : (result + current);
|
||||
}
|
||||
}
|
||||
@@ -13461,10 +13461,10 @@
|
||||
*/
|
||||
function add(augend, addend) {
|
||||
var result;
|
||||
if (augend === augend && augend != null) {
|
||||
if (augend !== undefined) {
|
||||
result = augend;
|
||||
}
|
||||
if (addend === addend && addend != null) {
|
||||
if (addend !== undefined) {
|
||||
result = result === undefined ? addend : (result + addend);
|
||||
}
|
||||
return result;
|
||||
@@ -13677,10 +13677,10 @@
|
||||
*/
|
||||
function subtract(minuend, subtrahend) {
|
||||
var result;
|
||||
if (minuend === minuend && minuend != null) {
|
||||
if (minuend !== undefined) {
|
||||
result = minuend;
|
||||
}
|
||||
if (subtrahend === subtrahend && subtrahend != null) {
|
||||
if (subtrahend !== undefined) {
|
||||
result = result === undefined ? subtrahend : (result - subtrahend);
|
||||
}
|
||||
return result;
|
||||
|
||||
18
test/test.js
18
test/test.js
@@ -17853,17 +17853,23 @@
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
QUnit.test('should skip `undefined` values', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.strictEqual(_.sum([1, undefined]), 1);
|
||||
});
|
||||
|
||||
QUnit.test('should not skip `NaN` values', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.deepEqual(_.sum([1, NaN]), NaN);
|
||||
});
|
||||
|
||||
QUnit.test('should not coerce values to numbers', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.strictEqual(_.sum(['1', '2']), '12');
|
||||
});
|
||||
|
||||
QUnit.test('should skip `null`, `undefined`, and `NaN` values', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.strictEqual(_.sum(['1', null, undefined, NaN, '2']), '12');
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user