1 | var baseSetData = require('./_baseSetData'),
|
2 | createBind = require('./_createBind'),
|
3 | createCurry = require('./_createCurry'),
|
4 | createHybrid = require('./_createHybrid'),
|
5 | createPartial = require('./_createPartial'),
|
6 | getData = require('./_getData'),
|
7 | mergeData = require('./_mergeData'),
|
8 | setData = require('./_setData'),
|
9 | setWrapToString = require('./_setWrapToString'),
|
10 | toInteger = require('./toInteger');
|
11 |
|
12 |
|
13 | var FUNC_ERROR_TEXT = 'Expected a function';
|
14 |
|
15 |
|
16 | var WRAP_BIND_FLAG = 1,
|
17 | WRAP_BIND_KEY_FLAG = 2,
|
18 | WRAP_CURRY_FLAG = 8,
|
19 | WRAP_CURRY_RIGHT_FLAG = 16,
|
20 | WRAP_PARTIAL_FLAG = 32,
|
21 | WRAP_PARTIAL_RIGHT_FLAG = 64;
|
22 |
|
23 |
|
24 | var nativeMax = Math.max;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
|
52 | var isBindKey = bitmask & WRAP_BIND_KEY_FLAG;
|
53 | if (!isBindKey && typeof func != 'function') {
|
54 | throw new TypeError(FUNC_ERROR_TEXT);
|
55 | }
|
56 | var length = partials ? partials.length : 0;
|
57 | if (!length) {
|
58 | bitmask &= ~(WRAP_PARTIAL_FLAG | WRAP_PARTIAL_RIGHT_FLAG);
|
59 | partials = holders = undefined;
|
60 | }
|
61 | ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
|
62 | arity = arity === undefined ? arity : toInteger(arity);
|
63 | length -= holders ? holders.length : 0;
|
64 |
|
65 | if (bitmask & WRAP_PARTIAL_RIGHT_FLAG) {
|
66 | var partialsRight = partials,
|
67 | holdersRight = holders;
|
68 |
|
69 | partials = holders = undefined;
|
70 | }
|
71 | var data = isBindKey ? undefined : getData(func);
|
72 |
|
73 | var newData = [
|
74 | func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
|
75 | argPos, ary, arity
|
76 | ];
|
77 |
|
78 | if (data) {
|
79 | mergeData(newData, data);
|
80 | }
|
81 | func = newData[0];
|
82 | bitmask = newData[1];
|
83 | thisArg = newData[2];
|
84 | partials = newData[3];
|
85 | holders = newData[4];
|
86 | arity = newData[9] = newData[9] === undefined
|
87 | ? (isBindKey ? 0 : func.length)
|
88 | : nativeMax(newData[9] - length, 0);
|
89 |
|
90 | if (!arity && bitmask & (WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG)) {
|
91 | bitmask &= ~(WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG);
|
92 | }
|
93 | if (!bitmask || bitmask == WRAP_BIND_FLAG) {
|
94 | var result = createBind(func, bitmask, thisArg);
|
95 | } else if (bitmask == WRAP_CURRY_FLAG || bitmask == WRAP_CURRY_RIGHT_FLAG) {
|
96 | result = createCurry(func, bitmask, arity);
|
97 | } else if ((bitmask == WRAP_PARTIAL_FLAG || bitmask == (WRAP_BIND_FLAG | WRAP_PARTIAL_FLAG)) && !holders.length) {
|
98 | result = createPartial(func, bitmask, thisArg, partials);
|
99 | } else {
|
100 | result = createHybrid.apply(undefined, newData);
|
101 | }
|
102 | var setter = data ? baseSetData : setData;
|
103 | return setWrapToString(setter(result, newData), func, bitmask);
|
104 | }
|
105 |
|
106 | module.exports = createWrap;
|