Make _.add, _.subtract, and _.sum not skip NaN values.

This commit is contained in:
John-David Dalton
2015-12-19 10:37:49 -06:00
parent 9e99a57615
commit 4b77b7a8b3
2 changed files with 17 additions and 11 deletions

View File

@@ -835,7 +835,7 @@
while (++index < length) { while (++index < length) {
var current = iteratee(array[index]); var current = iteratee(array[index]);
if (current === current && current != null) { if (current !== undefined) {
result = result === undefined ? current : (result + current); result = result === undefined ? current : (result + current);
} }
} }
@@ -13461,10 +13461,10 @@
*/ */
function add(augend, addend) { function add(augend, addend) {
var result; var result;
if (augend === augend && augend != null) { if (augend !== undefined) {
result = augend; result = augend;
} }
if (addend === addend && addend != null) { if (addend !== undefined) {
result = result === undefined ? addend : (result + addend); result = result === undefined ? addend : (result + addend);
} }
return result; return result;
@@ -13677,10 +13677,10 @@
*/ */
function subtract(minuend, subtrahend) { function subtract(minuend, subtrahend) {
var result; var result;
if (minuend === minuend && minuend != null) { if (minuend !== undefined) {
result = minuend; result = minuend;
} }
if (subtrahend === subtrahend && subtrahend != null) { if (subtrahend !== undefined) {
result = result === undefined ? subtrahend : (result - subtrahend); result = result === undefined ? subtrahend : (result - subtrahend);
} }
return result; return result;

View File

@@ -17853,17 +17853,23 @@
assert.deepEqual(actual, expected); 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) { QUnit.test('should not coerce values to numbers', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(_.sum(['1', '2']), '12'); 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');
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/