Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

_createWrap.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /** Error message constants. */
  12. var FUNC_ERROR_TEXT = 'Expected a function';
  13. /** Used to compose bitmasks for function metadata. */
  14. var WRAP_BIND_FLAG = 1,
  15. WRAP_BIND_KEY_FLAG = 2,
  16. WRAP_CURRY_FLAG = 8,
  17. WRAP_CURRY_RIGHT_FLAG = 16,
  18. WRAP_PARTIAL_FLAG = 32,
  19. WRAP_PARTIAL_RIGHT_FLAG = 64;
  20. /* Built-in method references for those with the same name as other `lodash` methods. */
  21. var nativeMax = Math.max;
  22. /**
  23. * Creates a function that either curries or invokes `func` with optional
  24. * `this` binding and partially applied arguments.
  25. *
  26. * @private
  27. * @param {Function|string} func The function or method name to wrap.
  28. * @param {number} bitmask The bitmask flags.
  29. * 1 - `_.bind`
  30. * 2 - `_.bindKey`
  31. * 4 - `_.curry` or `_.curryRight` of a bound function
  32. * 8 - `_.curry`
  33. * 16 - `_.curryRight`
  34. * 32 - `_.partial`
  35. * 64 - `_.partialRight`
  36. * 128 - `_.rearg`
  37. * 256 - `_.ary`
  38. * 512 - `_.flip`
  39. * @param {*} [thisArg] The `this` binding of `func`.
  40. * @param {Array} [partials] The arguments to be partially applied.
  41. * @param {Array} [holders] The `partials` placeholder indexes.
  42. * @param {Array} [argPos] The argument positions of the new function.
  43. * @param {number} [ary] The arity cap of `func`.
  44. * @param {number} [arity] The arity of `func`.
  45. * @returns {Function} Returns the new wrapped function.
  46. */
  47. function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
  48. var isBindKey = bitmask & WRAP_BIND_KEY_FLAG;
  49. if (!isBindKey && typeof func != 'function') {
  50. throw new TypeError(FUNC_ERROR_TEXT);
  51. }
  52. var length = partials ? partials.length : 0;
  53. if (!length) {
  54. bitmask &= ~(WRAP_PARTIAL_FLAG | WRAP_PARTIAL_RIGHT_FLAG);
  55. partials = holders = undefined;
  56. }
  57. ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
  58. arity = arity === undefined ? arity : toInteger(arity);
  59. length -= holders ? holders.length : 0;
  60. if (bitmask & WRAP_PARTIAL_RIGHT_FLAG) {
  61. var partialsRight = partials,
  62. holdersRight = holders;
  63. partials = holders = undefined;
  64. }
  65. var data = isBindKey ? undefined : getData(func);
  66. var newData = [
  67. func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
  68. argPos, ary, arity
  69. ];
  70. if (data) {
  71. mergeData(newData, data);
  72. }
  73. func = newData[0];
  74. bitmask = newData[1];
  75. thisArg = newData[2];
  76. partials = newData[3];
  77. holders = newData[4];
  78. arity = newData[9] = newData[9] === undefined
  79. ? (isBindKey ? 0 : func.length)
  80. : nativeMax(newData[9] - length, 0);
  81. if (!arity && bitmask & (WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG)) {
  82. bitmask &= ~(WRAP_CURRY_FLAG | WRAP_CURRY_RIGHT_FLAG);
  83. }
  84. if (!bitmask || bitmask == WRAP_BIND_FLAG) {
  85. var result = createBind(func, bitmask, thisArg);
  86. } else if (bitmask == WRAP_CURRY_FLAG || bitmask == WRAP_CURRY_RIGHT_FLAG) {
  87. result = createCurry(func, bitmask, arity);
  88. } else if ((bitmask == WRAP_PARTIAL_FLAG || bitmask == (WRAP_BIND_FLAG | WRAP_PARTIAL_FLAG)) && !holders.length) {
  89. result = createPartial(func, bitmask, thisArg, partials);
  90. } else {
  91. result = createHybrid.apply(undefined, newData);
  92. }
  93. var setter = data ? baseSetData : setData;
  94. return setWrapToString(setter(result, newData), func, bitmask);
  95. }
  96. module.exports = createWrap;