Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / createFlow.js
1 var LodashWrapper = require('./LodashWrapper'),
2     getData = require('./getData'),
3     getFuncName = require('./getFuncName'),
4     isArray = require('../lang/isArray'),
5     isLaziable = require('./isLaziable');
6
7 /** Used to compose bitmasks for wrapper metadata. */
8 var CURRY_FLAG = 8,
9     PARTIAL_FLAG = 32,
10     ARY_FLAG = 128,
11     REARG_FLAG = 256;
12
13 /** Used as the size to enable large array optimizations. */
14 var LARGE_ARRAY_SIZE = 200;
15
16 /** Used as the `TypeError` message for "Functions" methods. */
17 var FUNC_ERROR_TEXT = 'Expected a function';
18
19 /**
20  * Creates a `_.flow` or `_.flowRight` function.
21  *
22  * @private
23  * @param {boolean} [fromRight] Specify iterating from right to left.
24  * @returns {Function} Returns the new flow function.
25  */
26 function createFlow(fromRight) {
27   return function() {
28     var wrapper,
29         length = arguments.length,
30         index = fromRight ? length : -1,
31         leftIndex = 0,
32         funcs = Array(length);
33
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);
38       }
39       if (!wrapper && LodashWrapper.prototype.thru && getFuncName(func) == 'wrapper') {
40         wrapper = new LodashWrapper([], true);
41       }
42     }
43     index = wrapper ? -1 : length;
44     while (++index < length) {
45       func = funcs[index];
46
47       var funcName = getFuncName(func),
48           data = funcName == 'wrapper' ? getData(func) : undefined;
49
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]);
52       } else {
53         wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func);
54       }
55     }
56     return function() {
57       var args = arguments,
58           value = args[0];
59
60       if (wrapper && args.length == 1 && isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
61         return wrapper.plant(value).value();
62       }
63       var index = 0,
64           result = length ? funcs[index].apply(this, args) : value;
65
66       while (++index < length) {
67         result = funcs[index].call(this, result);
68       }
69       return result;
70     };
71   };
72 }
73
74 module.exports = createFlow;