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': [ '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', 'defaultTo', 'delay', 'difference', 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'defaultTo', 'delay',
'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'difference', 'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile',
'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast', 'endsWith', 'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast',
'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth', 'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth',
'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight',
'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', 'includes', 'indexOf', 'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', 'includes', 'indexOf',

View File

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

View File

@@ -836,34 +836,11 @@
QUnit.module('fp.defaultTo'); QUnit.module('fp.defaultTo');
(function() { (function() {
QUnit.test('should return a default value if input is undefined', function(assert) { QUnit.test('should have an argument order of `defaultValue` then `value`', 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); assert.expect(2);
var actual = fp.flow([ assert.strictEqual(fp.defaultTo(1)(0), 0);
fp.find(function(a) { return a > 3 }), assert.strictEqual(fp.defaultTo(1)(undefined), 1);
fp.defaultTo(0)
]);
assert.deepEqual(actual([1,2,3]), 0);
assert.deepEqual(actual([3,4,5]), 4);
}); });
}()); }());

View File

@@ -4604,15 +4604,18 @@
QUnit.module('lodash.defaultTo'); QUnit.module('lodash.defaultTo');
(function() { (function() {
QUnit.test('should return a default value if the input is undefined', function(assert) { QUnit.test('should return a default value if `value` is `undefined`', function(assert) {
assert.expect(3); assert.expect(1);
assert.strictEqual(_.defaultTo(1, 0), 1); var expected = lodashStable.map(falsey, function(value) {
assert.strictEqual(_.defaultTo(undefined, 0), 0); 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);
}); });
}()); }());