1 var LodashWrapper = require('./_LodashWrapper'),
2 flatRest = require('./_flatRest'),
3 getData = require('./_getData'),
4 getFuncName = require('./_getFuncName'),
5 isArray = require('./isArray'),
6 isLaziable = require('./_isLaziable');
8 /** Error message constants. */
9 var FUNC_ERROR_TEXT = 'Expected a function';
11 /** Used to compose bitmasks for function metadata. */
12 var WRAP_CURRY_FLAG = 8,
13 WRAP_PARTIAL_FLAG = 32,
15 WRAP_REARG_FLAG = 256;
18 * Creates a `_.flow` or `_.flowRight` function.
21 * @param {boolean} [fromRight] Specify iterating from right to left.
22 * @returns {Function} Returns the new flow function.
24 function createFlow(fromRight) {
25 return flatRest(function(funcs) {
26 var length = funcs.length,
28 prereq = LodashWrapper.prototype.thru;
34 var func = funcs[index];
35 if (typeof func != 'function') {
36 throw new TypeError(FUNC_ERROR_TEXT);
38 if (prereq && !wrapper && getFuncName(func) == 'wrapper') {
39 var wrapper = new LodashWrapper([], true);
42 index = wrapper ? index : length;
43 while (++index < length) {
46 var funcName = getFuncName(func),
47 data = funcName == 'wrapper' ? getData(func) : undefined;
49 if (data && isLaziable(data[0]) &&
50 data[1] == (WRAP_ARY_FLAG | WRAP_CURRY_FLAG | WRAP_PARTIAL_FLAG | WRAP_REARG_FLAG) &&
51 !data[4].length && data[9] == 1
53 wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
55 wrapper = (func.length == 1 && isLaziable(func))
64 if (wrapper && args.length == 1 && isArray(value)) {
65 return wrapper.plant(value).value();
68 result = length ? funcs[index].apply(this, args) : value;
70 while (++index < length) {
71 result = funcs[index].call(this, result);
78 module.exports = createFlow;