Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / mergeData.js
1 var arrayCopy = require('./arrayCopy'),
2     composeArgs = require('./composeArgs'),
3     composeArgsRight = require('./composeArgsRight'),
4     replaceHolders = require('./replaceHolders');
5
6 /** Used to compose bitmasks for wrapper metadata. */
7 var BIND_FLAG = 1,
8     CURRY_BOUND_FLAG = 4,
9     CURRY_FLAG = 8,
10     ARY_FLAG = 128,
11     REARG_FLAG = 256;
12
13 /** Used as the internal argument placeholder. */
14 var PLACEHOLDER = '__lodash_placeholder__';
15
16 /* Native method references for those with the same name as other `lodash` methods. */
17 var nativeMin = Math.min;
18
19 /**
20  * Merges the function metadata of `source` into `data`.
21  *
22  * Merging metadata reduces the number of wrappers required to invoke a function.
23  * This is possible because methods like `_.bind`, `_.curry`, and `_.partial`
24  * may be applied regardless of execution order. Methods like `_.ary` and `_.rearg`
25  * augment function arguments, making the order in which they are executed important,
26  * preventing the merging of metadata. However, we make an exception for a safe
27  * common case where curried functions have `_.ary` and or `_.rearg` applied.
28  *
29  * @private
30  * @param {Array} data The destination metadata.
31  * @param {Array} source The source metadata.
32  * @returns {Array} Returns `data`.
33  */
34 function mergeData(data, source) {
35   var bitmask = data[1],
36       srcBitmask = source[1],
37       newBitmask = bitmask | srcBitmask,
38       isCommon = newBitmask < ARY_FLAG;
39
40   var isCombo =
41     (srcBitmask == ARY_FLAG && bitmask == CURRY_FLAG) ||
42     (srcBitmask == ARY_FLAG && bitmask == REARG_FLAG && data[7].length <= source[8]) ||
43     (srcBitmask == (ARY_FLAG | REARG_FLAG) && bitmask == CURRY_FLAG);
44
45   // Exit early if metadata can't be merged.
46   if (!(isCommon || isCombo)) {
47     return data;
48   }
49   // Use source `thisArg` if available.
50   if (srcBitmask & BIND_FLAG) {
51     data[2] = source[2];
52     // Set when currying a bound function.
53     newBitmask |= (bitmask & BIND_FLAG) ? 0 : CURRY_BOUND_FLAG;
54   }
55   // Compose partial arguments.
56   var value = source[3];
57   if (value) {
58     var partials = data[3];
59     data[3] = partials ? composeArgs(partials, value, source[4]) : arrayCopy(value);
60     data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : arrayCopy(source[4]);
61   }
62   // Compose partial right arguments.
63   value = source[5];
64   if (value) {
65     partials = data[5];
66     data[5] = partials ? composeArgsRight(partials, value, source[6]) : arrayCopy(value);
67     data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : arrayCopy(source[6]);
68   }
69   // Use source `argPos` if available.
70   value = source[7];
71   if (value) {
72     data[7] = arrayCopy(value);
73   }
74   // Use source `ary` if it's smaller.
75   if (srcBitmask & ARY_FLAG) {
76     data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]);
77   }
78   // Use source `arity` if one is not provided.
79   if (data[9] == null) {
80     data[9] = source[9];
81   }
82   // Use source `func` and merge bitmasks.
83   data[0] = source[0];
84   data[1] = newBitmask;
85
86   return data;
87 }
88
89 module.exports = mergeData;