1 var arrayCopy = require('./arrayCopy'),
2 composeArgs = require('./composeArgs'),
3 composeArgsRight = require('./composeArgsRight'),
4 createCtorWrapper = require('./createCtorWrapper'),
5 isLaziable = require('./isLaziable'),
6 reorder = require('./reorder'),
7 replaceHolders = require('./replaceHolders'),
8 setData = require('./setData');
10 /** Used to compose bitmasks for wrapper metadata. */
15 CURRY_RIGHT_FLAG = 16,
17 PARTIAL_RIGHT_FLAG = 64,
20 /* Native method references for those with the same name as other `lodash` methods. */
21 var nativeMax = Math.max;
24 * Creates a function that wraps `func` and invokes it with optional `this`
25 * binding of, partial application, and currying.
28 * @param {Function|string} func The function or method name to reference.
29 * @param {number} bitmask The bitmask of flags. See `createWrapper` for more details.
30 * @param {*} [thisArg] The `this` binding of `func`.
31 * @param {Array} [partials] The arguments to prepend to those provided to the new function.
32 * @param {Array} [holders] The `partials` placeholder indexes.
33 * @param {Array} [partialsRight] The arguments to append to those provided to the new function.
34 * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
35 * @param {Array} [argPos] The argument positions of the new function.
36 * @param {number} [ary] The arity cap of `func`.
37 * @param {number} [arity] The arity of `func`.
38 * @returns {Function} Returns the new wrapped function.
40 function createHybridWrapper(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
41 var isAry = bitmask & ARY_FLAG,
42 isBind = bitmask & BIND_FLAG,
43 isBindKey = bitmask & BIND_KEY_FLAG,
44 isCurry = bitmask & CURRY_FLAG,
45 isCurryBound = bitmask & CURRY_BOUND_FLAG,
46 isCurryRight = bitmask & CURRY_RIGHT_FLAG,
47 Ctor = isBindKey ? undefined : createCtorWrapper(func);
50 // Avoid `arguments` object use disqualifying optimizations by
51 // converting it to an array before providing it to other functions.
52 var length = arguments.length,
57 args[index] = arguments[index];
60 args = composeArgs(args, partials, holders);
63 args = composeArgsRight(args, partialsRight, holdersRight);
65 if (isCurry || isCurryRight) {
66 var placeholder = wrapper.placeholder,
67 argsHolders = replaceHolders(args, placeholder);
69 length -= argsHolders.length;
71 var newArgPos = argPos ? arrayCopy(argPos) : undefined,
72 newArity = nativeMax(arity - length, 0),
73 newsHolders = isCurry ? argsHolders : undefined,
74 newHoldersRight = isCurry ? undefined : argsHolders,
75 newPartials = isCurry ? args : undefined,
76 newPartialsRight = isCurry ? undefined : args;
78 bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
79 bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
82 bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
84 var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
85 result = createHybridWrapper.apply(undefined, newData);
87 if (isLaziable(func)) {
88 setData(result, newData);
90 result.placeholder = placeholder;
94 var thisBinding = isBind ? thisArg : this,
95 fn = isBindKey ? thisBinding[func] : func;
98 args = reorder(args, argPos);
100 if (isAry && ary < args.length) {
103 if (this && this !== global && this instanceof wrapper) {
104 fn = Ctor || createCtorWrapper(func);
106 return fn.apply(thisBinding, args);
111 module.exports = createHybridWrapper;