Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / createHybridWrapper.js
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');
9
10 /** Used to compose bitmasks for wrapper metadata. */
11 var BIND_FLAG = 1,
12     BIND_KEY_FLAG = 2,
13     CURRY_BOUND_FLAG = 4,
14     CURRY_FLAG = 8,
15     CURRY_RIGHT_FLAG = 16,
16     PARTIAL_FLAG = 32,
17     PARTIAL_RIGHT_FLAG = 64,
18     ARY_FLAG = 128;
19
20 /* Native method references for those with the same name as other `lodash` methods. */
21 var nativeMax = Math.max;
22
23 /**
24  * Creates a function that wraps `func` and invokes it with optional `this`
25  * binding of, partial application, and currying.
26  *
27  * @private
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.
39  */
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);
48
49   function wrapper() {
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,
53         index = length,
54         args = Array(length);
55
56     while (index--) {
57       args[index] = arguments[index];
58     }
59     if (partials) {
60       args = composeArgs(args, partials, holders);
61     }
62     if (partialsRight) {
63       args = composeArgsRight(args, partialsRight, holdersRight);
64     }
65     if (isCurry || isCurryRight) {
66       var placeholder = wrapper.placeholder,
67           argsHolders = replaceHolders(args, placeholder);
68
69       length -= argsHolders.length;
70       if (length < arity) {
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;
77
78         bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
79         bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
80
81         if (!isCurryBound) {
82           bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
83         }
84         var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
85             result = createHybridWrapper.apply(undefined, newData);
86
87         if (isLaziable(func)) {
88           setData(result, newData);
89         }
90         result.placeholder = placeholder;
91         return result;
92       }
93     }
94     var thisBinding = isBind ? thisArg : this,
95         fn = isBindKey ? thisBinding[func] : func;
96
97     if (argPos) {
98       args = reorder(args, argPos);
99     }
100     if (isAry && ary < args.length) {
101       args.length = ary;
102     }
103     if (this && this !== global && this instanceof wrapper) {
104       fn = Ctor || createCtorWrapper(func);
105     }
106     return fn.apply(thisBinding, args);
107   }
108   return wrapper;
109 }
110
111 module.exports = createHybridWrapper;