From 68f3c43805f9bd2ff8d9a26f16cee895cfee921c Mon Sep 17 00:00:00 2001 From: jdalton Date: Tue, 3 Mar 2015 09:19:27 -0800 Subject: [PATCH] Add `createComposer` to reduce the definition of `_.flow` and `_.flowRight`. --- lodash.src.js | 77 +++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index ee05ffa87..fc208e5e8 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -3217,6 +3217,41 @@ return new SetCache(values); }; + /** + * Creates a function to compose other functions into a single function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new composer function. + */ + function createComposer(fromRight) { + return function() { + var length = arguments.length, + index = length, + fromIndex = fromRight ? length - 1 : 0; + + if (!length) { + return function() { return arguments[0]; }; + } + var funcs = Array(length); + while (index--) { + funcs[index] = arguments[index]; + if (typeof funcs[index] != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + return function() { + var index = fromIndex, + result = funcs[index].apply(this, arguments); + + while ((fromRight ? index-- : ++index < length)) { + result = funcs[index].call(this, result); + } + return result; + }; + }; + } + /** * Creates a function that produces compound words out of the words in a * given string. @@ -7588,26 +7623,7 @@ * addSquare(1, 2); * // => 9 */ - function flow() { - var funcs = arguments, - length = funcs.length; - - if (!length) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = 0, - result = funcs[index].apply(this, arguments); - - while (++index < length) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flow = createComposer(); /** * This method is like `_.flow` except that it creates a function that @@ -7629,26 +7645,7 @@ * addSquare(1, 2); * // => 9 */ - function flowRight() { - var funcs = arguments, - fromIndex = funcs.length - 1; - - if (fromIndex < 0) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = fromIndex, - result = funcs[index].apply(this, arguments); - - while (index--) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flowRight = createComposer(true); /** * Creates a function that memoizes the result of `func`. If `resolver` is