mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 00:57:48 +00:00
Simplify flow modules.
This commit is contained in:
22
flow.js
22
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;
|
||||
|
||||
Reference in New Issue
Block a user