From 454219fbfe461456f8ebe9f7a894ddeb4cf8c4e2 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 11 Jan 2017 08:11:00 -0800 Subject: [PATCH] Simplify flow modules. --- .internal/createFlow.js | 38 -------------------------------------- flow.js | 22 ++++++++++++++++++---- flowRight.js | 8 +++++--- 3 files changed, 23 insertions(+), 45 deletions(-) delete mode 100644 .internal/createFlow.js diff --git a/.internal/createFlow.js b/.internal/createFlow.js deleted file mode 100644 index 2897c82e2..000000000 --- a/.internal/createFlow.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Creates a `flow` or `flowRight` function. - * - * @private - * @param {boolean} [fromRight] Specify iterating from right to left. - * @returns {Function} Returns the new flow function. - */ -function createFlow(fromRight) { - return (...funcs) => { - const length = funcs.length; - - let index = length; - - if (fromRight) { - funcs.reverse(); - } - - while (index--) { - if (typeof funcs[index] !== 'function') { - throw new TypeError('Expected a function'); - } - } - - return function(...args) { - const value = args[0]; - let index = 0; - let result = length ? funcs[index].apply(this, args) : value; - - while (++index < length) { - result = funcs[index].call(this, result); - } - - return result; - }; - }; -} - -export default createFlow; diff --git a/flow.js b/flow.js index 11b37bc32..8b3e05cab 100644 --- a/flow.js +++ b/flow.js @@ -1,5 +1,3 @@ -import createFlow from './.internal/createFlow.js'; - /** * Creates a function that returns the result of invoking the given functions * with the `this` binding of the created function, where each successive @@ -7,7 +5,7 @@ import createFlow from './.internal/createFlow.js'; * * @since 3.0.0 * @category Util - * @param {...(Function|Function[])} [funcs] The functions to invoke. + * @param {Function[]} [funcs] The functions to invoke. * @returns {Function} Returns the new composite function. * @see flowRight * @example @@ -20,6 +18,22 @@ import createFlow from './.internal/createFlow.js'; * addSquare(1, 2); * // => 9 */ -const flow = createFlow(); +function flow(funcs) { + const length = funcs ? funcs.length : 0; + let index = length; + while (index--) { + if (typeof funcs[index] != 'function') { + throw new TypeError('Expected a function'); + } + } + return function(...args) { + let index = 0; + let result = length ? funcs[index].apply(this, args) : args[0]; + while (++index < length) { + result = funcs[index].call(this, result); + } + return result; + }; +} export default flow; diff --git a/flowRight.js b/flowRight.js index 7e60eae4c..8bf9becfa 100644 --- a/flowRight.js +++ b/flowRight.js @@ -1,4 +1,4 @@ -import createFlow from './.internal/createFlow.js'; +import flow from './flow.js'; /** * This method is like `flow` except that it creates a function that @@ -6,7 +6,7 @@ import createFlow from './.internal/createFlow.js'; * * @since 3.0.0 * @category Util - * @param {...(Function|Function[])} [funcs] The functions to invoke. + * @param {Function[]} [funcs] The functions to invoke. * @returns {Function} Returns the new composite function. * @see flow * @example @@ -19,6 +19,8 @@ import createFlow from './.internal/createFlow.js'; * addSquare(1, 2); * // => 9 */ -const flowRight = createFlow(true); +function flowRight(funcs) { + return flow(funcs.reverse()); +} export default flowRight;