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. */
19 CURRY_RIGHT_FLAG = 16,
21 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.
33 * The bitmask may be composed of the following flags:
36 * 4 - `_.curry` or `_.curryRight` of a bound function
40 * 64 - `_.partialRight`
44 * @param {*} [thisArg] The `this` binding of `func`.
45 * @param {Array} [partials] The arguments to be partially applied.
46 * @param {Array} [holders] The `partials` placeholder indexes.
47 * @param {Array} [argPos] The argument positions of the new function.
48 * @param {number} [ary] The arity cap of `func`.
49 * @param {number} [arity] The arity of `func`.
50 * @returns {Function} Returns the new wrapped function.
52 function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
53 var isBindKey = bitmask & BIND_KEY_FLAG;
54 if (!isBindKey && typeof func != 'function') {
55 throw new TypeError(FUNC_ERROR_TEXT);
57 var length = partials ? partials.length : 0;
59 bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
60 partials = holders = undefined;
62 ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
63 arity = arity === undefined ? arity : toInteger(arity);
64 length -= holders ? holders.length : 0;
66 if (bitmask & PARTIAL_RIGHT_FLAG) {
67 var partialsRight = partials,
68 holdersRight = holders;
70 partials = holders = undefined;
72 var data = isBindKey ? undefined : getData(func);
75 func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
80 mergeData(newData, data);
85 partials = newData[3];
87 arity = newData[9] = newData[9] == null
88 ? (isBindKey ? 0 : func.length)
89 : nativeMax(newData[9] - length, 0);
91 if (!arity && bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG)) {
92 bitmask &= ~(CURRY_FLAG | CURRY_RIGHT_FLAG);
94 if (!bitmask || bitmask == BIND_FLAG) {
95 var result = createBind(func, bitmask, thisArg);
96 } else if (bitmask == CURRY_FLAG || bitmask == CURRY_RIGHT_FLAG) {
97 result = createCurry(func, bitmask, arity);
98 } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !holders.length) {
99 result = createPartial(func, bitmask, thisArg, partials);
101 result = createHybrid.apply(undefined, newData);
103 var setter = data ? baseSetData : setData;
104 return setWrapToString(setter(result, newData), func, bitmask);
107 module.exports = createWrap;