_mergeData.js 3.1 KB

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