mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Simplify flow modules.
This commit is contained in:
@@ -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;
|
|
||||||
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
|
* Creates a function that returns the result of invoking the given functions
|
||||||
* with the `this` binding of the created function, where each successive
|
* 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
|
* @since 3.0.0
|
||||||
* @category Util
|
* @category Util
|
||||||
* @param {...(Function|Function[])} [funcs] The functions to invoke.
|
* @param {Function[]} [funcs] The functions to invoke.
|
||||||
* @returns {Function} Returns the new composite function.
|
* @returns {Function} Returns the new composite function.
|
||||||
* @see flowRight
|
* @see flowRight
|
||||||
* @example
|
* @example
|
||||||
@@ -20,6 +18,22 @@ import createFlow from './.internal/createFlow.js';
|
|||||||
* addSquare(1, 2);
|
* addSquare(1, 2);
|
||||||
* // => 9
|
* // => 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;
|
export default flow;
|
||||||
|
|||||||
@@ -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
|
* 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
|
* @since 3.0.0
|
||||||
* @category Util
|
* @category Util
|
||||||
* @param {...(Function|Function[])} [funcs] The functions to invoke.
|
* @param {Function[]} [funcs] The functions to invoke.
|
||||||
* @returns {Function} Returns the new composite function.
|
* @returns {Function} Returns the new composite function.
|
||||||
* @see flow
|
* @see flow
|
||||||
* @example
|
* @example
|
||||||
@@ -19,6 +19,8 @@ import createFlow from './.internal/createFlow.js';
|
|||||||
* addSquare(1, 2);
|
* addSquare(1, 2);
|
||||||
* // => 9
|
* // => 9
|
||||||
*/
|
*/
|
||||||
const flowRight = createFlow(true);
|
function flowRight(funcs) {
|
||||||
|
return flow(funcs.reverse());
|
||||||
|
}
|
||||||
|
|
||||||
export default flowRight;
|
export default flowRight;
|
||||||
|
|||||||
Reference in New Issue
Block a user