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 | /** Error message constants. */
|
---|
13 | var FUNC_ERROR_TEXT = 'Expected a function';
|
---|
14 |
|
---|
15 | /** Used to compose bitmasks for function metadata. */
|
---|
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 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
---|
24 | var nativeMax = Math.max;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Creates a function that either curries or invokes `func` with optional
|
---|
28 | * `this` binding and partially applied arguments.
|
---|
29 | *
|
---|
30 | * @private
|
---|
31 | * @param {Function|string} func The function or method name to wrap.
|
---|
32 | * @param {number} bitmask The bitmask flags.
|
---|
33 | * 1 - `_.bind`
|
---|
34 | * 2 - `_.bindKey`
|
---|
35 | * 4 - `_.curry` or `_.curryRight` of a bound function
|
---|
36 | * 8 - `_.curry`
|
---|
37 | * 16 - `_.curryRight`
|
---|
38 | * 32 - `_.partial`
|
---|
39 | * 64 - `_.partialRight`
|
---|
40 | * 128 - `_.rearg`
|
---|
41 | * 256 - `_.ary`
|
---|
42 | * 512 - `_.flip`
|
---|
43 | * @param {*} [thisArg] The `this` binding of `func`.
|
---|
44 | * @param {Array} [partials] The arguments to be partially applied.
|
---|
45 | * @param {Array} [holders] The `partials` placeholder indexes.
|
---|
46 | * @param {Array} [argPos] The argument positions of the new function.
|
---|
47 | * @param {number} [ary] The arity cap of `func`.
|
---|
48 | * @param {number} [arity] The arity of `func`.
|
---|
49 | * @returns {Function} Returns the new wrapped function.
|
---|
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;
|
---|