mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Cleanup _.modArgs.
This commit is contained in:
109
lodash.src.js
109
lodash.src.js
@@ -7387,73 +7387,6 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a function that runs each argument through a transform function.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @memberOf _
|
|
||||||
* @category Function
|
|
||||||
* @param {Function} func The function to wrap.
|
|
||||||
* @param {...Function} [transforms] The function to transform the corresponding argument.
|
|
||||||
* @returns {Function} Returns the new function.
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* var square = function(x) {
|
|
||||||
* return x * x;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* var double = function(x) {
|
|
||||||
* return x * 2;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* var fn = _.modArgs(function(x, y) {
|
|
||||||
* return [x , y];
|
|
||||||
* }, square, double);
|
|
||||||
*
|
|
||||||
* fn(1, 2); // => [1, 4]
|
|
||||||
* fn(5, 10); // => [25, 20]
|
|
||||||
*
|
|
||||||
* // Ensure proper argument types
|
|
||||||
*
|
|
||||||
* var defaultNumber = function(defaultValue, num) {
|
|
||||||
* return _.isUndefined(num) ? defaultValue : num;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* var alwaysArray = function(list) {
|
|
||||||
* var result = list;
|
|
||||||
*
|
|
||||||
* if (_.isUndefined(list)) {
|
|
||||||
* result = [];
|
|
||||||
* } else if (!_.isArray(list)) {
|
|
||||||
* result = [list];
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* return result;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* var fn = _.modArgs(function(x, y) {
|
|
||||||
* console.log(x, y);
|
|
||||||
* }, alwaysArray, _.partial(defaultNumber, 0));
|
|
||||||
*
|
|
||||||
* fn(); // => [], 0
|
|
||||||
* fn('hello'); // => ['hello'], 0
|
|
||||||
* fn([true], 15); // => [true], 15
|
|
||||||
*/
|
|
||||||
var modArgs = restParam(function(callback, transforms) {
|
|
||||||
var length = transforms.length;
|
|
||||||
|
|
||||||
return function() {
|
|
||||||
var index = -1;
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
arguments[index] = transforms[index].call(this, arguments[index]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return callback.apply(this, arguments);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function that accepts up to `n` arguments ignoring any
|
* Creates a function that accepts up to `n` arguments ignoring any
|
||||||
* additional arguments.
|
* additional arguments.
|
||||||
@@ -8070,6 +8003,46 @@
|
|||||||
return memoized;
|
return memoized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a function that runs each argument through a transform function.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Function
|
||||||
|
* @param {Function} func The function to wrap.
|
||||||
|
* @param {...Function} [transforms] The function to transform the corresponding argument.
|
||||||
|
* @returns {Function} Returns the new function.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* function doubled(n) {
|
||||||
|
* return n * 2;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* function square(n) {
|
||||||
|
* return n * n;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* var modded = _.modArgs(function(x, y) {
|
||||||
|
* return [x , y];
|
||||||
|
* }, square, doubled);
|
||||||
|
*
|
||||||
|
* modded(1, 2);
|
||||||
|
* // => [1, 4]
|
||||||
|
*
|
||||||
|
* modded(5, 10);
|
||||||
|
* // => [25, 20]
|
||||||
|
*/
|
||||||
|
var modArgs = restParam(function(func, transforms) {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function that negates the result of the predicate `func`. The
|
* Creates a function that negates the result of the predicate `func`. The
|
||||||
* `func` predicate is invoked with the `this` binding and arguments of the
|
* `func` predicate is invoked with the `this` binding and arguments of the
|
||||||
@@ -11913,7 +11886,6 @@
|
|||||||
// Add functions that return wrapped values when chaining.
|
// Add functions that return wrapped values when chaining.
|
||||||
lodash.after = after;
|
lodash.after = after;
|
||||||
lodash.ary = ary;
|
lodash.ary = ary;
|
||||||
lodash.modArgs = modArgs;
|
|
||||||
lodash.assign = assign;
|
lodash.assign = assign;
|
||||||
lodash.at = at;
|
lodash.at = at;
|
||||||
lodash.before = before;
|
lodash.before = before;
|
||||||
@@ -11969,6 +11941,7 @@
|
|||||||
lodash.method = method;
|
lodash.method = method;
|
||||||
lodash.methodOf = methodOf;
|
lodash.methodOf = methodOf;
|
||||||
lodash.mixin = mixin;
|
lodash.mixin = mixin;
|
||||||
|
lodash.modArgs = modArgs;
|
||||||
lodash.negate = negate;
|
lodash.negate = negate;
|
||||||
lodash.omit = omit;
|
lodash.omit = omit;
|
||||||
lodash.once = once;
|
lodash.once = once;
|
||||||
|
|||||||
77
test/test.js
77
test/test.js
@@ -62,6 +62,7 @@
|
|||||||
|
|
||||||
/** Math helpers. */
|
/** Math helpers. */
|
||||||
var add = function(x, y) { return x + y; },
|
var add = function(x, y) { return x + y; },
|
||||||
|
doubled = function(n) { return n * 2; },
|
||||||
square = function(n) { return n * n; };
|
square = function(n) { return n * n; };
|
||||||
|
|
||||||
/** Used to set property descriptors. */
|
/** Used to set property descriptors. */
|
||||||
@@ -959,52 +960,6 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.modArgs');
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
function double(x) {
|
|
||||||
return x * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
function square(x) {
|
|
||||||
return x * x;
|
|
||||||
}
|
|
||||||
|
|
||||||
function fn() {
|
|
||||||
return Array.prototype.slice.call(arguments, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
test('should transform each argument', 1, function() {
|
|
||||||
var wrapped = _.modArgs(fn, double, square);
|
|
||||||
var actual = wrapped(5, 10);
|
|
||||||
|
|
||||||
deepEqual(actual, [10, 100]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should not transform any argument greater than the number of transforms', 1, function() {
|
|
||||||
var wrapped = _.modArgs(fn, double, square);
|
|
||||||
var actual = wrapped(5, 10, 18);
|
|
||||||
|
|
||||||
deepEqual(actual, [10, 100, 18]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should not transform any arguments if no transforms are provided', 1, function() {
|
|
||||||
var wrapped = _.modArgs(fn);
|
|
||||||
var actual = wrapped(5, 10, 18);
|
|
||||||
|
|
||||||
deepEqual(actual, [5, 10, 18]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should not pass undefined if transforms > arguments', 1, function() {
|
|
||||||
var wrapped = _.modArgs(fn, double, _.identity);
|
|
||||||
var actual = wrapped(5);
|
|
||||||
|
|
||||||
deepEqual(actual, [10]);
|
|
||||||
});
|
|
||||||
}());
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
QUnit.module('lodash.ary');
|
QUnit.module('lodash.ary');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -11438,6 +11393,36 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.modArgs');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
function fn() {
|
||||||
|
return slice.call(arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('should transform each argument', 1, function() {
|
||||||
|
var modded = _.modArgs(fn, doubled, square);
|
||||||
|
deepEqual(modded(5, 10), [10, 100]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not transform any argument greater than the number of transforms', 1, function() {
|
||||||
|
var modded = _.modArgs(fn, doubled, square);
|
||||||
|
deepEqual(modded(5, 10, 18), [10, 100, 18]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not transform any arguments if no transforms are provided', 1, function() {
|
||||||
|
var modded = _.modArgs(fn);
|
||||||
|
deepEqual(modded(5, 10, 18), [5, 10, 18]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not pass `undefined` if there are more `transforms` than `arguments`', 1, function() {
|
||||||
|
var modded = _.modArgs(fn, doubled, _.identity);
|
||||||
|
deepEqual(modded(5), [10]);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.negate');
|
QUnit.module('lodash.negate');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user