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