Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / globule / node_modules / lodash / _createWrap.js
1 var baseSetData = require('./_baseSetData'),
2     createBind = require('./_createBind'),
3     createCurry = require('./_createCurry'),
4     createHybrid = require('./_createHybrid'),
5     createPartial = require('./_createPartial'),
6     getData = require('./_getData'),
7     mergeData = require('./_mergeData'),
8     setData = require('./_setData'),
9     setWrapToString = require('./_setWrapToString'),
10     toInteger = require('./toInteger');
11
12 /** Error message constants. */
13 var FUNC_ERROR_TEXT = 'Expected a function';
14
15 /** Used to compose bitmasks for function metadata. */
16 var BIND_FLAG = 1,
17     BIND_KEY_FLAG = 2,
18     CURRY_FLAG = 8,
19     CURRY_RIGHT_FLAG = 16,
20     PARTIAL_FLAG = 32,
21     PARTIAL_RIGHT_FLAG = 64;
22
23 /* Built-in method references for those with the same name as other `lodash` methods. */
24 var nativeMax = Math.max;
25
26 /**
27  * Creates a function that either curries or invokes `func` with optional
28  * `this` binding and partially applied arguments.
29  *
30  * @private
31  * @param {Function|string} func The function or method name to wrap.
32  * @param {number} bitmask The bitmask flags.
33  *  The bitmask may be composed of the following flags:
34  *     1 - `_.bind`
35  *     2 - `_.bindKey`
36  *     4 - `_.curry` or `_.curryRight` of a bound function
37  *     8 - `_.curry`
38  *    16 - `_.curryRight`
39  *    32 - `_.partial`
40  *    64 - `_.partialRight`
41  *   128 - `_.rearg`
42  *   256 - `_.ary`
43  *   512 - `_.flip`
44  * @param {*} [thisArg] The `this` binding of `func`.
45  * @param {Array} [partials] The arguments to be partially applied.
46  * @param {Array} [holders] The `partials` placeholder indexes.
47  * @param {Array} [argPos] The argument positions of the new function.
48  * @param {number} [ary] The arity cap of `func`.
49  * @param {number} [arity] The arity of `func`.
50  * @returns {Function} Returns the new wrapped function.
51  */
52 function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
53   var isBindKey = bitmask & BIND_KEY_FLAG;
54   if (!isBindKey && typeof func != 'function') {
55     throw new TypeError(FUNC_ERROR_TEXT);
56   }
57   var length = partials ? partials.length : 0;
58   if (!length) {
59     bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
60     partials = holders = undefined;
61   }
62   ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
63   arity = arity === undefined ? arity : toInteger(arity);
64   length -= holders ? holders.length : 0;
65
66   if (bitmask & PARTIAL_RIGHT_FLAG) {
67     var partialsRight = partials,
68         holdersRight = holders;
69
70     partials = holders = undefined;
71   }
72   var data = isBindKey ? undefined : getData(func);
73
74   var newData = [
75     func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
76     argPos, ary, arity
77   ];
78
79   if (data) {
80     mergeData(newData, data);
81   }
82   func = newData[0];
83   bitmask = newData[1];
84   thisArg = newData[2];
85   partials = newData[3];
86   holders = newData[4];
87   arity = newData[9] = newData[9] == null
88     ? (isBindKey ? 0 : func.length)
89     : nativeMax(newData[9] - length, 0);
90
91   if (!arity && bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG)) {
92     bitmask &= ~(CURRY_FLAG | CURRY_RIGHT_FLAG);
93   }
94   if (!bitmask || bitmask == BIND_FLAG) {
95     var result = createBind(func, bitmask, thisArg);
96   } else if (bitmask == CURRY_FLAG || bitmask == CURRY_RIGHT_FLAG) {
97     result = createCurry(func, bitmask, arity);
98   } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !holders.length) {
99     result = createPartial(func, bitmask, thisArg, partials);
100   } else {
101     result = createHybrid.apply(undefined, newData);
102   }
103   var setter = data ? baseSetData : setData;
104   return setWrapToString(setter(result, newData), func, bitmask);
105 }
106
107 module.exports = createWrap;