From 4b77b7a8b3b294577eaf8a4f5f33119c46506733 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 19 Dec 2015 10:37:49 -0600 Subject: [PATCH] Make `_.add`, `_.subtract`, and `_.sum` not skip `NaN` values. --- lodash.js | 10 +++++----- test/test.js | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lodash.js b/lodash.js index 25d234ace..f40dfa9d2 100644 --- a/lodash.js +++ b/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; diff --git a/test/test.js b/test/test.js index 062bee68a..f415c67df 100644 --- a/test/test.js +++ b/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'); - }); }()); /*--------------------------------------------------------------------------*/