From 02a28d565bb6de439ed9ad55ce7f7539f7aec6f2 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 8 Sep 2015 22:48:37 -0700 Subject: [PATCH] Add `_.flip`. [closes #1449] --- lodash.js | 28 +++++++++++++++++++++++++++- test/test.js | 3 ++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index b17ae1da9..724469099 100644 --- a/lodash.js +++ b/lodash.js @@ -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; diff --git a/test/test.js b/test/test.js index e2b982652..96fcd8ee5 100644 --- a/test/test.js +++ b/test/test.js @@ -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)),