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 /** Used as the size to enable large array optimizations. */
9 var LARGE_ARRAY_SIZE = 200;
11 /** Error message constants. */
12 var FUNC_ERROR_TEXT = 'Expected a function';
14 /** Used to compose bitmasks for function metadata. */
21 * Creates a `_.flow` or `_.flowRight` function.
24 * @param {boolean} [fromRight] Specify iterating from right to left.
25 * @returns {Function} Returns the new flow function.
27 function createFlow(fromRight) {
28 return flatRest(function(funcs) {
29 var length = funcs.length,
31 prereq = LodashWrapper.prototype.thru;
37 var func = funcs[index];
38 if (typeof func != 'function') {
39 throw new TypeError(FUNC_ERROR_TEXT);
41 if (prereq && !wrapper && getFuncName(func) == 'wrapper') {
42 var wrapper = new LodashWrapper([], true);
45 index = wrapper ? index : length;
46 while (++index < length) {
49 var funcName = getFuncName(func),
50 data = funcName == 'wrapper' ? getData(func) : undefined;
52 if (data && isLaziable(data[0]) &&
53 data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) &&
54 !data[4].length && data[9] == 1
56 wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
58 wrapper = (func.length == 1 && isLaziable(func))
67 if (wrapper && args.length == 1 &&
68 isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
69 return wrapper.plant(value).value();
72 result = length ? funcs[index].apply(this, args) : value;
74 while (++index < length) {
75 result = funcs[index].call(this, result);
82 module.exports = createFlow;