Cleanup _.defaultTo.

This commit is contained in:
John-David Dalton
2016-05-25 08:41:51 -07:00
parent 5fbc5303ff
commit 6e67ebab05
4 changed files with 33 additions and 58 deletions

View File

@@ -62,9 +62,9 @@ exports.aryMethod = {
'2': [
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll',
'bindKey', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN',
'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'defaultTo', 'delay', 'difference',
'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith',
'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast',
'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',
'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight',
'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', 'includes', 'indexOf',

View File

@@ -1399,13 +1399,13 @@
*
* The wrapper methods that are **not** chainable by default are:
* `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`,
* `cloneDeep`, `cloneDeepWith`, `cloneWith`, `deburr`, `divide`, `each`,
* `eachRight`, `endsWith`, `eq`, `escape`, `escapeRegExp`, `every`, `find`,
* `findIndex`, `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `first`,
* `floor`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`,
* `forOwnRight`, `get`, `gt`, `gte`, `has`, `hasIn`, `head`, `identity`,
* `includes`, `indexOf`, `inRange`, `invoke`, `isArguments`, `isArray`,
* `isArrayBuffer`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`,
* `cloneDeep`, `cloneDeepWith`, `cloneWith`, `deburr`, `defaultTo`, `divide`,
* `each`, `eachRight`, `endsWith`, `eq`, `escape`, `escapeRegExp`, `every`,
* `find`, `findIndex`, `findKey`, `findLast`, `findLastIndex`, `findLastKey`,
* `first`, `floor`, `forEach`, `forEachRight`, `forIn`, `forInRight`,
* `forOwn`, `forOwnRight`, `get`, `gt`, `gte`, `has`, `hasIn`, `head`,
* `identity`, `includes`, `indexOf`, `inRange`, `invoke`, `isArguments`,
* `isArray`, `isArrayBuffer`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`,
* `isBuffer`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`,
* `isError`, `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMap`,
* `isMatch`, `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`,
@@ -12404,7 +12404,7 @@
/**
* Gets the value at `path` of `object`. If the resolved value is
* `undefined`, the `defaultValue` is used in its place.
* `undefined`, the `defaultValue` is returned in its place.
*
* @static
* @memberOf _
@@ -14706,28 +14706,23 @@
}
/**
* Returns a default value if the input is `undefined`.
* Checks `value` to determine if a default value should be returned.
* If `value` is `undefined`, the `defaultValue` is returned in its place.
*
* @static
* @memberOf _
* @since 4.13.2
* @since 4.14.0
* @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.
* @param {*} value The value to check.
* @param {*} defaultValue The default value.
* @returns {*} Returns the resolved value.
* @example
*
* console.log(_.defaultTo(undefined, 10));
* _.defaultTo(1, 10);
* // => 1
*
* _.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;

View File

@@ -836,34 +836,11 @@
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) {
QUnit.test('should have an argument order of `defaultValue` then `value`', 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);
assert.strictEqual(fp.defaultTo(1)(0), 0);
assert.strictEqual(fp.defaultTo(1)(undefined), 1);
});
}());

View File

@@ -4604,15 +4604,18 @@
QUnit.module('lodash.defaultTo');
(function() {
QUnit.test('should return a default value if the input is undefined', function(assert) {
assert.expect(3);
QUnit.test('should return a default value if `value` is `undefined`', function(assert) {
assert.expect(1);
assert.strictEqual(_.defaultTo(1, 0), 1);
assert.strictEqual(_.defaultTo(undefined, 0), 0);
var expected = lodashStable.map(falsey, function(value) {
return value === undefined ? 1 : value;
});
var actual = _.defaultTo(_.find([1,2,3], function(n) { return n > 5; }), 0);
var actual = lodashStable.map(falsey, function(value) {
return _.defaultTo(value, 1);
});
assert.strictEqual(actual, 0);
assert.deepEqual(actual, expected);
});
}());