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