diff --git a/fp/_mapping.js b/fp/_mapping.js index 2f62274a3..e03d0e79d 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -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', diff --git a/lodash.js b/lodash.js index 5840049a6..79994d789 100644 --- a/lodash.js +++ b/lodash.js @@ -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; diff --git a/test/test-fp.js b/test/test-fp.js index 8e8ca3263..05839e6ba 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -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); }); }()); diff --git a/test/test.js b/test/test.js index 4da14f93b..7f39fbb5a 100644 --- a/test/test.js +++ b/test/test.js @@ -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); }); }());