mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 04:17:49 +00:00
Cleanup _.spread docs & tests.
This commit is contained in:
@@ -7580,39 +7580,42 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function accepting an array as argument, that invokes `func`
|
* Creates a function that invokes `func` with the `this` binding of `thisArg`
|
||||||
* with arguments taken from this array, like `Function#apply`.
|
* and the array of arguments provided to the created function much like
|
||||||
* This works obviously well with `Promise.all`.
|
* [Function#apply](http://es5.github.io/#x15.3.4.3).
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Function
|
* @category Function
|
||||||
* @param {Function} The function to alter.
|
* @param {Function} The function to spread arguments over.
|
||||||
* @param {*} [thisArg] The `this` binding of `func`.
|
* @param {*} [thisArg] The `this` binding of `func`.
|
||||||
* @returns {*} Returns the new function.
|
* @returns {*} Returns the new function.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* var spread = _.spread(function (who, what) {
|
* var spread = _.spread(function(who, what) {
|
||||||
* return who + ' says ' + 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([
|
* var numbers = Promise.all([
|
||||||
* Promise.resolve(42),
|
* Promise.resolve(40),
|
||||||
* Promise.resolve(33)
|
* Promise.resolve(36)
|
||||||
* ])
|
* ]);
|
||||||
*
|
*
|
||||||
* numbers.then(_.spread(function (a, b) { return a + b })) // Promise of 75
|
* var add = function(x, y) {
|
||||||
* // instead of...
|
* return x + y;
|
||||||
* numbers.then(function (nums) { return nums[0] + nums[1] })
|
* };
|
||||||
|
*
|
||||||
|
* numbers.then(_.spread(add));
|
||||||
|
* // => a Promise of 76
|
||||||
*/
|
*/
|
||||||
function spread (func, thisArg) {
|
function spread(func, thisArg) {
|
||||||
if (typeof func !== 'function') {
|
if (typeof func != 'function') {
|
||||||
throw new TypeError(FUNC_ERROR_TEXT);
|
throw new TypeError(FUNC_ERROR_TEXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(array) {
|
return function(array) {
|
||||||
return func.apply(thisArg, array);
|
return func.apply(thisArg, array);
|
||||||
};
|
};
|
||||||
|
|||||||
53
test/test.js
53
test/test.js
@@ -12243,45 +12243,36 @@
|
|||||||
QUnit.module('lodash.spread');
|
QUnit.module('lodash.spread');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
function Pair(x, y) {
|
function add(x, y) {
|
||||||
this.x = x;
|
return x + y;
|
||||||
this.y = y;
|
|
||||||
|
|
||||||
return 42;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function divide(n, d) {
|
test('should spread arguments to `func`', 1, function() {
|
||||||
return n / d;
|
var spread = _.spread(add);
|
||||||
}
|
strictEqual(spread([4, 2]), 6);
|
||||||
|
|
||||||
test('should pass arguments to `func`', 1, function() {
|
|
||||||
var spread = _.spread(divide);
|
|
||||||
strictEqual(spread([4, 2]), 2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should fail when receiving non-array argument', 1, function() {
|
test('should throw a TypeError when receiving a non-array `array` argument', 1, function() {
|
||||||
var spread = _.spread(divide);
|
raises(function() { _.spread(4, 2); }, TypeError);
|
||||||
var err;
|
|
||||||
try {
|
|
||||||
spread(4, 2);
|
|
||||||
} catch (e) {
|
|
||||||
err = e;
|
|
||||||
}
|
|
||||||
strictEqual(_.isError(err), true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should dynamically `func` to `thisArg`', 2, function() {
|
test('should provide the correct `func` arguments', 1, function() {
|
||||||
var self = {z: 3};
|
var args;
|
||||||
var spread = _.spread(Pair, self);
|
|
||||||
var result = spread([1, 2]);
|
var spread = _.spread(function() {
|
||||||
strictEqual(result, 42);
|
args = slice.call(arguments);
|
||||||
deepEqual(self, {x: 1, y: 2, z: 3});
|
});
|
||||||
|
|
||||||
|
spread([4, 2], 'ignored');
|
||||||
|
deepEqual(args, [4, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should use `undefined` for extra values', 1, function() {
|
test('should support the `thisArg` argument', 1, function() {
|
||||||
var self = {};
|
var spread = _.spread(function(x, y) {
|
||||||
_.spread(Pair, self)([1]);
|
return this[x] + this[y];
|
||||||
deepEqual(self, {x: 1, y: undefined});
|
}, [4, 2]);
|
||||||
|
|
||||||
|
strictEqual(spread([0, 1]), 6);
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user