Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / createWrapper.js
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');
8
9 /** Used to compose bitmasks for wrapper metadata. */
10 var BIND_FLAG = 1,
11     BIND_KEY_FLAG = 2,
12     PARTIAL_FLAG = 32,
13     PARTIAL_RIGHT_FLAG = 64;
14
15 /** Used as the `TypeError` message for "Functions" methods. */
16 var FUNC_ERROR_TEXT = 'Expected a function';
17
18 /* Native method references for those with the same name as other `lodash` methods. */
19 var nativeMax = Math.max;
20
21 /**
22  * Creates a function that either curries or invokes `func` with optional
23  * `this` binding and partially applied arguments.
24  *
25  * @private
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:
29  *     1 - `_.bind`
30  *     2 - `_.bindKey`
31  *     4 - `_.curry` or `_.curryRight` of a bound function
32  *     8 - `_.curry`
33  *    16 - `_.curryRight`
34  *    32 - `_.partial`
35  *    64 - `_.partialRight`
36  *   128 - `_.rearg`
37  *   256 - `_.ary`
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.
45  */
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);
50   }
51   var length = partials ? partials.length : 0;
52   if (!length) {
53     bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
54     partials = holders = undefined;
55   }
56   length -= (holders ? holders.length : 0);
57   if (bitmask & PARTIAL_RIGHT_FLAG) {
58     var partialsRight = partials,
59         holdersRight = holders;
60
61     partials = holders = undefined;
62   }
63   var data = isBindKey ? undefined : getData(func),
64       newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity];
65
66   if (data) {
67     mergeData(newData, data);
68     bitmask = newData[1];
69     arity = newData[9];
70   }
71   newData[9] = arity == null
72     ? (isBindKey ? 0 : func.length)
73     : (nativeMax(arity - length, 0) || 0);
74
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);
79   } else {
80     result = createHybridWrapper.apply(undefined, newData);
81   }
82   var setter = data ? baseSetData : setData;
83   return setter(result, newData);
84 }
85
86 module.exports = createWrapper;