1 var baseSetData = require('./_baseSetData'),
2 createBaseWrapper = require('./_createBaseWrapper'),
3 createCurryWrapper = require('./_createCurryWrapper'),
4 createHybridWrapper = require('./_createHybridWrapper'),
5 createPartialWrapper = require('./_createPartialWrapper'),
6 getData = require('./_getData'),
7 mergeData = require('./_mergeData'),
8 setData = require('./_setData'),
9 toInteger = require('./toInteger');
11 /** Used to compose bitmasks for wrapper metadata. */
15 CURRY_RIGHT_FLAG = 16,
17 PARTIAL_RIGHT_FLAG = 64;
19 /** Used as the `TypeError` message for "Functions" methods. */
20 var FUNC_ERROR_TEXT = 'Expected a function';
22 /* Built-in method references for those with the same name as other `lodash` methods. */
23 var nativeMax = Math.max;
26 * Creates a function that either curries or invokes `func` with optional
27 * `this` binding and partially applied arguments.
30 * @param {Function|string} func The function or method name to wrap.
31 * @param {number} bitmask The bitmask of wrapper flags.
32 * The bitmask may be composed of the following flags:
35 * 4 - `_.curry` or `_.curryRight` of a bound function
39 * 64 - `_.partialRight`
42 * @param {*} [thisArg] The `this` binding of `func`.
43 * @param {Array} [partials] The arguments to be partially applied.
44 * @param {Array} [holders] The `partials` placeholder indexes.
45 * @param {Array} [argPos] The argument positions of the new function.
46 * @param {number} [ary] The arity cap of `func`.
47 * @param {number} [arity] The arity of `func`.
48 * @returns {Function} Returns the new wrapped function.
50 function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
51 var isBindKey = bitmask & BIND_KEY_FLAG;
52 if (!isBindKey && typeof func != 'function') {
53 throw new TypeError(FUNC_ERROR_TEXT);
55 var length = partials ? partials.length : 0;
57 bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
58 partials = holders = undefined;
60 ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
61 arity = arity === undefined ? arity : toInteger(arity);
62 length -= holders ? holders.length : 0;
64 if (bitmask & PARTIAL_RIGHT_FLAG) {
65 var partialsRight = partials,
66 holdersRight = holders;
68 partials = holders = undefined;
70 var data = isBindKey ? undefined : getData(func),
71 newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity];
74 mergeData(newData, data);
79 partials = newData[3];
81 arity = newData[9] = newData[9] == null
82 ? (isBindKey ? 0 : func.length)
83 : nativeMax(newData[9] - length, 0);
85 if (!arity && bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG)) {
86 bitmask &= ~(CURRY_FLAG | CURRY_RIGHT_FLAG);
88 if (!bitmask || bitmask == BIND_FLAG) {
89 var result = createBaseWrapper(func, bitmask, thisArg);
90 } else if (bitmask == CURRY_FLAG || bitmask == CURRY_RIGHT_FLAG) {
91 result = createCurryWrapper(func, bitmask, arity);
92 } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !holders.length) {
93 result = createPartialWrapper(func, bitmask, thisArg, partials);
95 result = createHybridWrapper.apply(undefined, newData);
97 var setter = data ? baseSetData : setData;
98 return setter(result, newData);
101 module.exports = createWrapper;