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