_createRecurry.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var isLaziable = require('./_isLaziable'),
  2. setData = require('./_setData'),
  3. setWrapToString = require('./_setWrapToString');
  4. /** Used to compose bitmasks for function metadata. */
  5. var WRAP_BIND_FLAG = 1,
  6. WRAP_BIND_KEY_FLAG = 2,
  7. WRAP_CURRY_BOUND_FLAG = 4,
  8. WRAP_CURRY_FLAG = 8,
  9. WRAP_PARTIAL_FLAG = 32,
  10. WRAP_PARTIAL_RIGHT_FLAG = 64;
  11. /**
  12. * Creates a function that wraps `func` to continue currying.
  13. *
  14. * @private
  15. * @param {Function} func The function to wrap.
  16. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  17. * @param {Function} wrapFunc The function to create the `func` wrapper.
  18. * @param {*} placeholder The placeholder value.
  19. * @param {*} [thisArg] The `this` binding of `func`.
  20. * @param {Array} [partials] The arguments to prepend to those provided to
  21. * the new function.
  22. * @param {Array} [holders] The `partials` placeholder indexes.
  23. * @param {Array} [argPos] The argument positions of the new function.
  24. * @param {number} [ary] The arity cap of `func`.
  25. * @param {number} [arity] The arity of `func`.
  26. * @returns {Function} Returns the new wrapped function.
  27. */
  28. function createRecurry(func, bitmask, wrapFunc, placeholder, thisArg, partials, holders, argPos, ary, arity) {
  29. var isCurry = bitmask & WRAP_CURRY_FLAG,
  30. newHolders = isCurry ? holders : undefined,
  31. newHoldersRight = isCurry ? undefined : holders,
  32. newPartials = isCurry ? partials : undefined,
  33. newPartialsRight = isCurry ? undefined : partials;
  34. bitmask |= (isCurry ? WRAP_PARTIAL_FLAG : WRAP_PARTIAL_RIGHT_FLAG);
  35. bitmask &= ~(isCurry ? WRAP_PARTIAL_RIGHT_FLAG : WRAP_PARTIAL_FLAG);
  36. if (!(bitmask & WRAP_CURRY_BOUND_FLAG)) {
  37. bitmask &= ~(WRAP_BIND_FLAG | WRAP_BIND_KEY_FLAG);
  38. }
  39. var newData = [
  40. func, bitmask, thisArg, newPartials, newHolders, newPartialsRight,
  41. newHoldersRight, argPos, ary, arity
  42. ];
  43. var result = wrapFunc.apply(undefined, newData);
  44. if (isLaziable(func)) {
  45. setData(result, newData);
  46. }
  47. result.placeholder = placeholder;
  48. return setWrapToString(result, func, bitmask);
  49. }
  50. module.exports = createRecurry;