Added _.defaultTo. (#2381)

This commit is contained in:
Mateo Calle
2016-05-24 20:05:37 -05:00
committed by John-David Dalton
parent e582ad226a
commit 5fbc5303ff
4 changed files with 85 additions and 2 deletions

View File

@@ -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',

View File

@@ -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;

View File

@@ -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() {

View File

@@ -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);