From 5fbc5303ff7e8349d63f9c803d51ef141693f9e0 Mon Sep 17 00:00:00 2001 From: Mateo Calle Date: Tue, 24 May 2016 20:05:37 -0500 Subject: [PATCH] Added _.defaultTo. (#2381) --- fp/_mapping.js | 2 +- lodash.js | 29 +++++++++++++++++++++++++++++ test/test-fp.js | 36 ++++++++++++++++++++++++++++++++++++ test/test.js | 20 +++++++++++++++++++- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/fp/_mapping.js b/fp/_mapping.js index a30c5dee4..2f62274a3 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -62,7 +62,7 @@ exports.aryMethod = { '2': [ 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll', 'bindKey', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN', - 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference', + 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'defaultTo', 'delay', 'difference', 'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast', 'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth', diff --git a/lodash.js b/lodash.js index 70fd86c4c..5840049a6 100644 --- a/lodash.js +++ b/lodash.js @@ -14705,6 +14705,34 @@ }; } + /** + * Returns a default value if the input is `undefined`. + * + * @static + * @memberOf _ + * @since 4.13.2 + * @category Util + * @param {*} input The value to be checked if it is `undefined`. + * @param {*} defaultValue The default value that gets returned if the input is `undefined`. + * @returns {*} Returns the input or if it's `undefined`, the default value. + * @example + * + * console.log(_.defaultTo(undefined, 10)); + * // => 10 + * + * // Inside flow (FP only) + * var findOrDefault = _.flow([ + * _.find(function(number) { number > 5; }), + * _.defaultTo(0) + * ]); + * + * findOrDefault([1,2,3]); + * // => 0 + */ + function defaultTo(value, defaultValue) { + return value === undefined ? defaultValue : value; + } + /** * Creates a function that returns the result of invoking the given functions * with the `this` binding of the created function, where each successive @@ -15979,6 +16007,7 @@ lodash.cloneDeepWith = cloneDeepWith; lodash.cloneWith = cloneWith; lodash.deburr = deburr; + lodash.defaultTo = defaultTo; lodash.divide = divide; lodash.endsWith = endsWith; lodash.eq = eq; diff --git a/test/test-fp.js b/test/test-fp.js index 1fd668753..8e8ca3263 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -833,6 +833,42 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('fp.defaultTo'); + + (function() { + QUnit.test('should return a default value if input is undefined', function(assert) { + assert.expect(4); + + // default (inverse params) + var actual = fp.defaultTo(0, fp.find(function(a) { return a > 1; }, [1,2,3])); + assert.deepEqual(actual, 2); + + var actual = fp.defaultTo(0, fp.find(function(a) { return a > 5; }, [1,2,3])); + assert.deepEqual(actual, 0); + + // curried + var actual = fp.defaultTo(0)(fp.find(function(a) { return a > 1; })([1,2,3])); + assert.deepEqual(actual, 2); + + var actual = fp.defaultTo(0)(fp.find(function(a) { return a > 5; })([1,2,3])); + assert.deepEqual(actual, 0); + }); + + QUnit.test('should work in a flow', function(assert) { + assert.expect(2); + + var actual = fp.flow([ + fp.find(function(a) { return a > 3 }), + fp.defaultTo(0) + ]); + + assert.deepEqual(actual([1,2,3]), 0); + assert.deepEqual(actual([3,4,5]), 4); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('fp.difference'); (function() { diff --git a/test/test.js b/test/test.js index 1006fbc5c..4da14f93b 100644 --- a/test/test.js +++ b/test/test.js @@ -4601,6 +4601,23 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.defaultTo'); + + (function() { + QUnit.test('should return a default value if the input is undefined', function(assert) { + assert.expect(3); + + assert.strictEqual(_.defaultTo(1, 0), 1); + assert.strictEqual(_.defaultTo(undefined, 0), 0); + + var actual = _.defaultTo(_.find([1,2,3], function(n) { return n > 5; }), 0); + + assert.strictEqual(actual, 0); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.defer'); (function() { @@ -26246,6 +26263,7 @@ 'ceil', 'clone', 'deburr', + 'defaultTo', 'divide', 'endsWith', 'escape', @@ -26576,7 +26594,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(314); + assert.expect(315); var arrays = lodashStable.map(falsey, stubArray);