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 |
|
9 | var FUNC_ERROR_TEXT = 'Expected a function';
|
10 |
|
11 |
|
12 | var WRAP_CURRY_FLAG = 8,
|
13 | WRAP_PARTIAL_FLAG = 32,
|
14 | WRAP_ARY_FLAG = 128,
|
15 | WRAP_REARG_FLAG = 256;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function createFlow(fromRight) {
|
25 | return flatRest(function(funcs) {
|
26 | var length = funcs.length,
|
27 | index = length,
|
28 | prereq = LodashWrapper.prototype.thru;
|
29 |
|
30 | if (fromRight) {
|
31 | funcs.reverse();
|
32 | }
|
33 | while (index--) {
|
34 | var func = funcs[index];
|
35 | if (typeof func != 'function') {
|
36 | throw new TypeError(FUNC_ERROR_TEXT);
|
37 | }
|
38 | if (prereq && !wrapper && getFuncName(func) == 'wrapper') {
|
39 | var wrapper = new LodashWrapper([], true);
|
40 | }
|
41 | }
|
42 | index = wrapper ? index : length;
|
43 | while (++index < length) {
|
44 | func = funcs[index];
|
45 |
|
46 | var funcName = getFuncName(func),
|
47 | data = funcName == 'wrapper' ? getData(func) : undefined;
|
48 |
|
49 | if (data && isLaziable(data[0]) &&
|
50 | data[1] == (WRAP_ARY_FLAG | WRAP_CURRY_FLAG | WRAP_PARTIAL_FLAG | WRAP_REARG_FLAG) &&
|
51 | !data[4].length && data[9] == 1
|
52 | ) {
|
53 | wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
|
54 | } else {
|
55 | wrapper = (func.length == 1 && isLaziable(func))
|
56 | ? wrapper[funcName]()
|
57 | : wrapper.thru(func);
|
58 | }
|
59 | }
|
60 | return function() {
|
61 | var args = arguments,
|
62 | value = args[0];
|
63 |
|
64 | if (wrapper && args.length == 1 && isArray(value)) {
|
65 | return wrapper.plant(value).value();
|
66 | }
|
67 | var index = 0,
|
68 | result = length ? funcs[index].apply(this, args) : value;
|
69 |
|
70 | while (++index < length) {
|
71 | result = funcs[index].call(this, result);
|
72 | }
|
73 | return result;
|
74 | };
|
75 | });
|
76 | }
|
77 |
|
78 | module.exports = createFlow;
|