1 var LodashWrapper = require('./LodashWrapper'),
2 getData = require('./getData'),
3 getFuncName = require('./getFuncName'),
4 isArray = require('../lang/isArray'),
5 isLaziable = require('./isLaziable');
7 /** Used to compose bitmasks for wrapper metadata. */
13 /** Used as the size to enable large array optimizations. */
14 var LARGE_ARRAY_SIZE = 200;
16 /** Used as the `TypeError` message for "Functions" methods. */
17 var FUNC_ERROR_TEXT = 'Expected a function';
20 * Creates a `_.flow` or `_.flowRight` function.
23 * @param {boolean} [fromRight] Specify iterating from right to left.
24 * @returns {Function} Returns the new flow function.
26 function createFlow(fromRight) {
29 length = arguments.length,
30 index = fromRight ? length : -1,
32 funcs = Array(length);
34 while ((fromRight ? index-- : ++index < length)) {
35 var func = funcs[leftIndex++] = arguments[index];
36 if (typeof func != 'function') {
37 throw new TypeError(FUNC_ERROR_TEXT);
39 if (!wrapper && LodashWrapper.prototype.thru && getFuncName(func) == 'wrapper') {
40 wrapper = new LodashWrapper([], true);
43 index = wrapper ? -1 : length;
44 while (++index < length) {
47 var funcName = getFuncName(func),
48 data = funcName == 'wrapper' ? getData(func) : undefined;
50 if (data && isLaziable(data[0]) && data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) && !data[4].length && data[9] == 1) {
51 wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
53 wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func);
60 if (wrapper && args.length == 1 && isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
61 return wrapper.plant(value).value();
64 result = length ? funcs[index].apply(this, args) : value;
66 while (++index < length) {
67 result = funcs[index].call(this, result);
74 module.exports = createFlow;