mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Use a create function helper for modArgs.
This commit is contained in:
40
lodash.js
40
lodash.js
@@ -3513,6 +3513,31 @@
|
|||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a function like `_.modArgs`.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Function} resolver The function to resolve which invocation
|
||||||
|
* arguments are provided to each transform.
|
||||||
|
* @returns {Function} Returns the new arguments modifier function.
|
||||||
|
*/
|
||||||
|
function createModArgs(resolver) {
|
||||||
|
return restParam(function(func, transforms) {
|
||||||
|
transforms = baseFlatten(transforms);
|
||||||
|
if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) {
|
||||||
|
throw new TypeError(FUNC_ERROR_TEXT);
|
||||||
|
}
|
||||||
|
var length = transforms.length;
|
||||||
|
return restParam(function(args) {
|
||||||
|
var index = nativeMin(args.length, length);
|
||||||
|
while (index--) {
|
||||||
|
args[index] = transforms[index].apply(this, resolver(args[index], index, args));
|
||||||
|
}
|
||||||
|
return func.apply(this, args);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the padding required for `string` based on the given `length`.
|
* Creates the padding required for `string` based on the given `length`.
|
||||||
* The `chars` string is truncated if the number of characters exceeds `length`.
|
* The `chars` string is truncated if the number of characters exceeds `length`.
|
||||||
@@ -7578,19 +7603,8 @@
|
|||||||
* modded(5, 10);
|
* modded(5, 10);
|
||||||
* // => [25, 20]
|
* // => [25, 20]
|
||||||
*/
|
*/
|
||||||
var modArgs = restParam(function(func, transforms) {
|
var modArgs = createModArgs(function(value) {
|
||||||
transforms = baseFlatten(transforms);
|
return [value];
|
||||||
if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) {
|
|
||||||
throw new TypeError(FUNC_ERROR_TEXT);
|
|
||||||
}
|
|
||||||
var length = transforms.length;
|
|
||||||
return restParam(function(args) {
|
|
||||||
var index = nativeMin(args.length, length);
|
|
||||||
while (index--) {
|
|
||||||
args[index] = transforms[index](args[index]);
|
|
||||||
}
|
|
||||||
return func.apply(this, args);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11057,19 +11057,19 @@
|
|||||||
deepEqual(modded(5, 10, 18), [5, 10, 18]);
|
deepEqual(modded(5, 10, 18), [5, 10, 18]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not pass `undefined` if there are more `transforms` than `arguments`', 1, function() {
|
test('should not pass `undefined` if there are more transforms than arguments', 1, function() {
|
||||||
var modded = _.modArgs(fn, doubled, _.identity);
|
var modded = _.modArgs(fn, doubled, _.identity);
|
||||||
deepEqual(modded(5), [10]);
|
deepEqual(modded(5), [10]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not set a `this` binding', 1, function() {
|
test('should use `this` binding of function for transforms', 1, function() {
|
||||||
var modded = _.modArgs(function(x) {
|
var modded = _.modArgs(function(x) {
|
||||||
return this[x];
|
return this[x];
|
||||||
}, function(x) {
|
}, function(x) {
|
||||||
return this === x;
|
return this === x;
|
||||||
});
|
});
|
||||||
|
|
||||||
var object = { 'modded': modded, 'false': 1 };
|
var object = { 'modded': modded, 'true': 1 };
|
||||||
strictEqual(object.modded(object), 1);
|
strictEqual(object.modded(object), 1);
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user