1 var baseSetData = require('./baseSetData'),
2 createBindWrapper = require('./createBindWrapper'),
3 createHybridWrapper = require('./createHybridWrapper'),
4 createPartialWrapper = require('./createPartialWrapper'),
5 getData = require('./getData'),
6 mergeData = require('./mergeData'),
7 setData = require('./setData');
9 /** Used to compose bitmasks for wrapper metadata. */
13 PARTIAL_RIGHT_FLAG = 64;
15 /** Used as the `TypeError` message for "Functions" methods. */
16 var FUNC_ERROR_TEXT = 'Expected a function';
18 /* Native method references for those with the same name as other `lodash` methods. */
19 var nativeMax = Math.max;
22 * Creates a function that either curries or invokes `func` with optional
23 * `this` binding and partially applied arguments.
26 * @param {Function|string} func The function or method name to reference.
27 * @param {number} bitmask The bitmask of flags.
28 * The bitmask may be composed of the following flags:
31 * 4 - `_.curry` or `_.curryRight` of a bound function
35 * 64 - `_.partialRight`
38 * @param {*} [thisArg] The `this` binding of `func`.
39 * @param {Array} [partials] The arguments to be partially applied.
40 * @param {Array} [holders] The `partials` placeholder indexes.
41 * @param {Array} [argPos] The argument positions of the new function.
42 * @param {number} [ary] The arity cap of `func`.
43 * @param {number} [arity] The arity of `func`.
44 * @returns {Function} Returns the new wrapped function.
46 function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
47 var isBindKey = bitmask & BIND_KEY_FLAG;
48 if (!isBindKey && typeof func != 'function') {
49 throw new TypeError(FUNC_ERROR_TEXT);
51 var length = partials ? partials.length : 0;
53 bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
54 partials = holders = undefined;
56 length -= (holders ? holders.length : 0);
57 if (bitmask & PARTIAL_RIGHT_FLAG) {
58 var partialsRight = partials,
59 holdersRight = holders;
61 partials = holders = undefined;
63 var data = isBindKey ? undefined : getData(func),
64 newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity];
67 mergeData(newData, data);
71 newData[9] = arity == null
72 ? (isBindKey ? 0 : func.length)
73 : (nativeMax(arity - length, 0) || 0);
75 if (bitmask == BIND_FLAG) {
76 var result = createBindWrapper(newData[0], newData[2]);
77 } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !newData[4].length) {
78 result = createPartialWrapper.apply(undefined, newData);
80 result = createHybridWrapper.apply(undefined, newData);
82 var setter = data ? baseSetData : setData;
83 return setter(result, newData);
86 module.exports = createWrapper;