From 90f95306f9b5d38ff6144fe259b0c1559a18655f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 25 May 2016 13:00:10 -0700 Subject: [PATCH] Make `_.defaultTo` return the `defaultValue` for `NaN` and nullish values. --- lodash.js | 7 ++++--- test/test.js | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index deccdddf1..4f0cdc1fb 100644 --- a/lodash.js +++ b/lodash.js @@ -14706,8 +14706,9 @@ } /** - * Checks `value` to determine if a default value should be returned. - * If `value` is `undefined`, the `defaultValue` is returned in its place. + * Checks `value` to determine whether a default value should be returned in + * its place. The `defaultValue` is returned if `value` is `NaN`, `null`, + * or `undefined`. * * @static * @memberOf _ @@ -14725,7 +14726,7 @@ * // => 10 */ function defaultTo(value, defaultValue) { - return value === undefined ? defaultValue : value; + return (value == null || value !== value) ? defaultValue : value; } /** diff --git a/test/test.js b/test/test.js index 42e4b434e..8beac98b0 100644 --- a/test/test.js +++ b/test/test.js @@ -4604,11 +4604,11 @@ QUnit.module('lodash.defaultTo'); (function() { - QUnit.test('should return a default value if `value` is `undefined`', function(assert) { + QUnit.test('should return a default value if `value` is `NaN` or nullish', function(assert) { assert.expect(1); var expected = lodashStable.map(falsey, function(value) { - return value === undefined ? 1 : value; + return (value == null || value !== value) ? 1 : value; }); var actual = lodashStable.map(falsey, function(value) {