_createCurry.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var apply = require('./_apply'),
  2. createCtor = require('./_createCtor'),
  3. createHybrid = require('./_createHybrid'),
  4. createRecurry = require('./_createRecurry'),
  5. getHolder = require('./_getHolder'),
  6. replaceHolders = require('./_replaceHolders'),
  7. root = require('./_root');
  8. /**
  9. * Creates a function that wraps `func` to enable currying.
  10. *
  11. * @private
  12. * @param {Function} func The function to wrap.
  13. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  14. * @param {number} arity The arity of `func`.
  15. * @returns {Function} Returns the new wrapped function.
  16. */
  17. function createCurry(func, bitmask, arity) {
  18. var Ctor = createCtor(func);
  19. function wrapper() {
  20. var length = arguments.length,
  21. args = Array(length),
  22. index = length,
  23. placeholder = getHolder(wrapper);
  24. while (index--) {
  25. args[index] = arguments[index];
  26. }
  27. var holders = (length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder)
  28. ? []
  29. : replaceHolders(args, placeholder);
  30. length -= holders.length;
  31. if (length < arity) {
  32. return createRecurry(
  33. func, bitmask, createHybrid, wrapper.placeholder, undefined,
  34. args, holders, undefined, undefined, arity - length);
  35. }
  36. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  37. return apply(fn, this, args);
  38. }
  39. return wrapper;
  40. }
  41. module.exports = createCurry;