mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Added _.defaultTo. (#2381)
This commit is contained in:
committed by
John-David Dalton
parent
e582ad226a
commit
5fbc5303ff
@@ -62,7 +62,7 @@ exports.aryMethod = {
|
|||||||
'2': [
|
'2': [
|
||||||
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll',
|
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll',
|
||||||
'bindKey', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN',
|
'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',
|
'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith',
|
||||||
'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast',
|
'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast',
|
||||||
'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth',
|
'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth',
|
||||||
|
|||||||
29
lodash.js
29
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
|
* Creates a function that returns the result of invoking the given functions
|
||||||
* with the `this` binding of the created function, where each successive
|
* with the `this` binding of the created function, where each successive
|
||||||
@@ -15979,6 +16007,7 @@
|
|||||||
lodash.cloneDeepWith = cloneDeepWith;
|
lodash.cloneDeepWith = cloneDeepWith;
|
||||||
lodash.cloneWith = cloneWith;
|
lodash.cloneWith = cloneWith;
|
||||||
lodash.deburr = deburr;
|
lodash.deburr = deburr;
|
||||||
|
lodash.defaultTo = defaultTo;
|
||||||
lodash.divide = divide;
|
lodash.divide = divide;
|
||||||
lodash.endsWith = endsWith;
|
lodash.endsWith = endsWith;
|
||||||
lodash.eq = eq;
|
lodash.eq = eq;
|
||||||
|
|||||||
@@ -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');
|
QUnit.module('fp.difference');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
20
test/test.js
20
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');
|
QUnit.module('lodash.defer');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -26246,6 +26263,7 @@
|
|||||||
'ceil',
|
'ceil',
|
||||||
'clone',
|
'clone',
|
||||||
'deburr',
|
'deburr',
|
||||||
|
'defaultTo',
|
||||||
'divide',
|
'divide',
|
||||||
'endsWith',
|
'endsWith',
|
||||||
'escape',
|
'escape',
|
||||||
@@ -26576,7 +26594,7 @@
|
|||||||
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
QUnit.test('should accept falsey arguments', function(assert) {
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
assert.expect(314);
|
assert.expect(315);
|
||||||
|
|
||||||
var arrays = lodashStable.map(falsey, stubArray);
|
var arrays = lodashStable.map(falsey, stubArray);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user