7d34c74916548fe5783374057ffb85a16e8e6a42
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / _createHybridWrapper.js
1 var composeArgs = require('./_composeArgs'),
2     composeArgsRight = require('./_composeArgsRight'),
3     createCtorWrapper = require('./_createCtorWrapper'),
4     createRecurryWrapper = require('./_createRecurryWrapper'),
5     reorder = require('./_reorder'),
6     replaceHolders = require('./_replaceHolders'),
7     root = require('./_root');
8
9 /** Used to compose bitmasks for wrapper metadata. */
10 var BIND_FLAG = 1,
11     BIND_KEY_FLAG = 2,
12     CURRY_FLAG = 8,
13     CURRY_RIGHT_FLAG = 16,
14     ARY_FLAG = 128,
15     FLIP_FLAG = 512;
16
17 /**
18  * Creates a function that wraps `func` to invoke it with optional `this`
19  * binding of `thisArg`, partial application, and currying.
20  *
21  * @private
22  * @param {Function|string} func The function or method name to wrap.
23  * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper` for more details.
24  * @param {*} [thisArg] The `this` binding of `func`.
25  * @param {Array} [partials] The arguments to prepend to those provided to the new function.
26  * @param {Array} [holders] The `partials` placeholder indexes.
27  * @param {Array} [partialsRight] The arguments to append to those provided to the new function.
28  * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
29  * @param {Array} [argPos] The argument positions of the new function.
30  * @param {number} [ary] The arity cap of `func`.
31  * @param {number} [arity] The arity of `func`.
32  * @returns {Function} Returns the new wrapped function.
33  */
34 function createHybridWrapper(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
35   var isAry = bitmask & ARY_FLAG,
36       isBind = bitmask & BIND_FLAG,
37       isBindKey = bitmask & BIND_KEY_FLAG,
38       isCurry = bitmask & CURRY_FLAG,
39       isCurryRight = bitmask & CURRY_RIGHT_FLAG,
40       isFlip = bitmask & FLIP_FLAG,
41       Ctor = isBindKey ? undefined : createCtorWrapper(func);
42
43   function wrapper() {
44     var length = arguments.length,
45         index = length,
46         args = Array(length);
47
48     while (index--) {
49       args[index] = arguments[index];
50     }
51     if (partials) {
52       args = composeArgs(args, partials, holders);
53     }
54     if (partialsRight) {
55       args = composeArgsRight(args, partialsRight, holdersRight);
56     }
57     if (isCurry || isCurryRight) {
58       var placeholder = wrapper.placeholder,
59           argsHolders = replaceHolders(args, placeholder);
60
61       length -= argsHolders.length;
62       if (length < arity) {
63         return createRecurryWrapper(func, bitmask, createHybridWrapper, placeholder, thisArg, args, argsHolders, argPos, ary, arity - length);
64       }
65     }
66     var thisBinding = isBind ? thisArg : this,
67         fn = isBindKey ? thisBinding[func] : func;
68
69     if (argPos) {
70       args = reorder(args, argPos);
71     } else if (isFlip && args.length > 1) {
72       args.reverse();
73     }
74     if (isAry && ary < args.length) {
75       args.length = ary;
76     }
77     if (this && this !== root && this instanceof wrapper) {
78       fn = Ctor || createCtorWrapper(fn);
79     }
80     return fn.apply(thisBinding, args);
81   }
82   return wrapper;
83 }
84
85 module.exports = createHybridWrapper;