1 var LodashWrapper = require('./_LodashWrapper'),
2 baseFlatten = require('./_baseFlatten'),
3 getData = require('./_getData'),
4 getFuncName = require('./_getFuncName'),
5 isArray = require('./isArray'),
6 isLaziable = require('./_isLaziable'),
7 rest = require('./rest');
9 /** Used to compose bitmasks for wrapper metadata. */
15 /** Used as the size to enable large array optimizations. */
16 var LARGE_ARRAY_SIZE = 200;
18 /** Used as the `TypeError` message for "Functions" methods. */
19 var FUNC_ERROR_TEXT = 'Expected a function';
22 * Creates a `_.flow` or `_.flowRight` function.
25 * @param {boolean} [fromRight] Specify iterating from right to left.
26 * @returns {Function} Returns the new flow function.
28 function createFlow(fromRight) {
29 return rest(function(funcs) {
30 funcs = baseFlatten(funcs);
32 var length = funcs.length,
34 prereq = LodashWrapper.prototype.thru;
40 var func = funcs[index];
41 if (typeof func != 'function') {
42 throw new TypeError(FUNC_ERROR_TEXT);
44 if (prereq && !wrapper && getFuncName(func) == 'wrapper') {
45 var wrapper = new LodashWrapper([], true);
48 index = wrapper ? index : length;
49 while (++index < length) {
52 var funcName = getFuncName(func),
53 data = funcName == 'wrapper' ? getData(func) : undefined;
55 if (data && isLaziable(data[0]) && data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) && !data[4].length && data[9] == 1) {
56 wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
58 wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func);
65 if (wrapper && args.length == 1 && isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
66 return wrapper.plant(value).value();
69 result = length ? funcs[index].apply(this, args) : value;
71 while (++index < length) {
72 result = funcs[index].call(this, result);
79 module.exports = createFlow;