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