_createRecurryWrapper.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var isLaziable = require('./_isLaziable'),
  2. setData = require('./_setData');
  3. /** Used to compose bitmasks for wrapper metadata. */
  4. var BIND_FLAG = 1,
  5. BIND_KEY_FLAG = 2,
  6. CURRY_BOUND_FLAG = 4,
  7. CURRY_FLAG = 8,
  8. PARTIAL_FLAG = 32,
  9. PARTIAL_RIGHT_FLAG = 64;
  10. /**
  11. * Creates a function that wraps `func` to continue currying.
  12. *
  13. * @private
  14. * @param {Function} func The function to wrap.
  15. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  16. * 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 createRecurryWrapper(func, bitmask, wrapFunc, placeholder, thisArg, partials, holders, argPos, ary, arity) {
  29. var isCurry = bitmask & 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 ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
  35. bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
  36. if (!(bitmask & CURRY_BOUND_FLAG)) {
  37. bitmask &= ~(BIND_FLAG | 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 result;
  49. }
  50. module.exports = createRecurryWrapper;