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');
12 /** Error message constants. */
13 var FUNC_ERROR_TEXT = 'Expected a function';
15 /** Used to compose bitmasks for function metadata. */
16 var WRAP_BIND_FLAG = 1,
17 WRAP_BIND_KEY_FLAG = 2,
19 WRAP_CURRY_RIGHT_FLAG = 16,
20 WRAP_PARTIAL_FLAG = 32,
21 WRAP_PARTIAL_RIGHT_FLAG = 64;
23 /* Built-in method references for those with the same name as other `lodash` methods. */
24 var nativeMax = Math.max;
27 * Creates a function that either curries or invokes `func` with optional
28 * `this` binding and partially applied arguments.
31 * @param {Function|string} func The function or method name to wrap.
32 * @param {number} bitmask The bitmask flags.
35 * 4 - `_.curry` or `_.curryRight` of a bound function
39 * 64 - `_.partialRight`
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.
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);
56 var length = partials ? partials.length : 0;
58 bitmask &= ~(WRAP_PARTIAL_FLAG | WRAP_PARTIAL_RIGHT_FLAG);
59 partials = holders = undefined;
61 ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
62 arity = arity === undefined ? arity : toInteger(arity);
63 length -= holders ? holders.length : 0;
65 if (bitmask & WRAP_PARTIAL_RIGHT_FLAG) {
66 var partialsRight = partials,
67 holdersRight = holders;
69 partials = holders = undefined;
71 var data = isBindKey ? undefined : getData(func);
74 func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
79 mergeData(newData, data);
84 partials = newData[3];
86 arity = newData[9] = newData[9] === undefined
87 ? (isBindKey ? 0 : func.length)
88 : nativeMax(newData[9] - length, 0);
90 if (!arity && bitmask & (WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG)) {
91 bitmask &= ~(WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG);
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);
100 result = createHybrid.apply(undefined, newData);
102 var setter = data ? baseSetData : setData;
103 return setWrapToString(setter(result, newData), func, bitmask);
106 module.exports = createWrap;