diff --git a/lodash.src.js b/lodash.src.js index dbe7c46d1..387a36728 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -7580,39 +7580,42 @@ } /** - * Creates a function accepting an array as argument, that invokes `func` - * with arguments taken from this array, like `Function#apply`. - * This works obviously well with `Promise.all`. + * Creates a function that invokes `func` with the `this` binding of `thisArg` + * and the array of arguments provided to the created function much like + * [Function#apply](http://es5.github.io/#x15.3.4.3). * * @static * @memberOf _ * @category Function - * @param {Function} The function to alter. + * @param {Function} The function to spread arguments over. * @param {*} [thisArg] The `this` binding of `func`. * @returns {*} Returns the new function. * @example * - * var spread = _.spread(function (who, what) { + * var spread = _.spread(function(who, what) { * return who + ' says ' + what; * }); * - * spread(['John', 'hello']) // => 'John says hello' + * spread(['Fred', 'hello']); + * // => 'Fred says hello' * - * // With Promise + * // with a Promise * var numbers = Promise.all([ - * Promise.resolve(42), - * Promise.resolve(33) - * ]) + * Promise.resolve(40), + * Promise.resolve(36) + * ]); * - * numbers.then(_.spread(function (a, b) { return a + b })) // Promise of 75 - * // instead of... - * numbers.then(function (nums) { return nums[0] + nums[1] }) + * var add = function(x, y) { + * return x + y; + * }; + * + * numbers.then(_.spread(add)); + * // => a Promise of 76 */ - function spread (func, thisArg) { - if (typeof func !== 'function') { + function spread(func, thisArg) { + if (typeof func != 'function') { throw new TypeError(FUNC_ERROR_TEXT); } - return function(array) { return func.apply(thisArg, array); }; diff --git a/test/test.js b/test/test.js index 03b30c98f..10bba4d21 100644 --- a/test/test.js +++ b/test/test.js @@ -12243,45 +12243,36 @@ QUnit.module('lodash.spread'); (function() { - function Pair(x, y) { - this.x = x; - this.y = y; - - return 42; + function add(x, y) { + return x + y; } - function divide(n, d) { - return n / d; - } - - test('should pass arguments to `func`', 1, function() { - var spread = _.spread(divide); - strictEqual(spread([4, 2]), 2); + test('should spread arguments to `func`', 1, function() { + var spread = _.spread(add); + strictEqual(spread([4, 2]), 6); }); - test('should fail when receiving non-array argument', 1, function() { - var spread = _.spread(divide); - var err; - try { - spread(4, 2); - } catch (e) { - err = e; - } - strictEqual(_.isError(err), true); + test('should throw a TypeError when receiving a non-array `array` argument', 1, function() { + raises(function() { _.spread(4, 2); }, TypeError); }); - test('should dynamically `func` to `thisArg`', 2, function() { - var self = {z: 3}; - var spread = _.spread(Pair, self); - var result = spread([1, 2]); - strictEqual(result, 42); - deepEqual(self, {x: 1, y: 2, z: 3}); + test('should provide the correct `func` arguments', 1, function() { + var args; + + var spread = _.spread(function() { + args = slice.call(arguments); + }); + + spread([4, 2], 'ignored'); + deepEqual(args, [4, 2]); }); - test('should use `undefined` for extra values', 1, function() { - var self = {}; - _.spread(Pair, self)([1]); - deepEqual(self, {x: 1, y: undefined}); + test('should support the `thisArg` argument', 1, function() { + var spread = _.spread(function(x, y) { + return this[x] + this[y]; + }, [4, 2]); + + strictEqual(spread([0, 1]), 6); }); }());