Add _.flip. [closes #1449]

This commit is contained in:
John-David Dalton
2015-09-08 22:48:37 -07:00
parent 2ac6e31cc3
commit 02a28d565b
2 changed files with 29 additions and 2 deletions

View File

@@ -23,7 +23,8 @@
PARTIAL_FLAG = 32,
PARTIAL_RIGHT_FLAG = 64,
ARY_FLAG = 128,
REARG_FLAG = 256;
REARG_FLAG = 256,
FLIP_FLAG = 512;
/** Used to compose bitmasks for comparison styles. */
var UNORDERED_COMPARE_FLAG = 1,
@@ -3453,6 +3454,7 @@
isCurry = bitmask & CURRY_FLAG,
isCurryBound = bitmask & CURRY_BOUND_FLAG,
isCurryRight = bitmask & CURRY_RIGHT_FLAG,
isFlip = bitmask & FLIP_FLAG,
Ctor = isBindKey ? undefined : createCtorWrapper(func);
function wrapper() {
@@ -3505,6 +3507,8 @@
if (argPos) {
args = reorder(args, argPos);
} else if (isFlip) {
args.reverse();
}
if (isAry && ary < args.length) {
args.length = ary;
@@ -7471,6 +7475,27 @@
return baseDelay(func, wait, args);
});
/**
* Creates a function that invokes `func` with arguments reversed.
*
* @static
* @memberOf _
* @category Function
* @param {Function} func The function to flip arguments for.
* @returns {Function} Returns the new function.
* @example
*
* var flipped = _.flip(function() {
* return _.toArray(arguments);
* });
*
* flipped('a', 'b', 'c')
* // => ['c', 'b', 'a']
*/
function flip(func) {
return createWrapper(func, FLIP_FLAG);
}
/**
* Creates a function that returns the result of invoking the provided
* functions with the `this` binding of the created function, where each
@@ -11854,6 +11879,7 @@
lodash.filter = filter;
lodash.flatten = flatten;
lodash.flattenDeep = flattenDeep;
lodash.flip = flip;
lodash.flow = flow;
lodash.flowRight = flowRight;
lodash.functions = functions;

View File

@@ -20617,6 +20617,7 @@
'debounce',
'defer',
'delay',
'flip',
'memoize',
'modArgs',
'modArgsSet',
@@ -20740,7 +20741,7 @@
});
QUnit.test('should throw an error for falsey arguments', function(assert) {
assert.expect(24);
assert.expect(25);
_.each(rejectFalsey, function(methodName) {
var expected = _.map(falsey, _.constant(true)),