_createPartialWrapper.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var apply = require('./_apply'),
  2. createCtorWrapper = require('./_createCtorWrapper'),
  3. root = require('./_root');
  4. /** Used to compose bitmasks for wrapper metadata. */
  5. var BIND_FLAG = 1;
  6. /**
  7. * Creates a function that wraps `func` to invoke it with the `this` binding
  8. * of `thisArg` and `partials` prepended to the arguments it receives.
  9. *
  10. * @private
  11. * @param {Function} func The function to wrap.
  12. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  13. * for more details.
  14. * @param {*} thisArg The `this` binding of `func`.
  15. * @param {Array} partials The arguments to prepend to those provided to
  16. * the new function.
  17. * @returns {Function} Returns the new wrapped function.
  18. */
  19. function createPartialWrapper(func, bitmask, thisArg, partials) {
  20. var isBind = bitmask & BIND_FLAG,
  21. Ctor = createCtorWrapper(func);
  22. function wrapper() {
  23. var argsIndex = -1,
  24. argsLength = arguments.length,
  25. leftIndex = -1,
  26. leftLength = partials.length,
  27. args = Array(leftLength + argsLength),
  28. fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  29. while (++leftIndex < leftLength) {
  30. args[leftIndex] = partials[leftIndex];
  31. }
  32. while (argsLength--) {
  33. args[leftIndex++] = arguments[++argsIndex];
  34. }
  35. return apply(fn, isBind ? thisArg : this, args);
  36. }
  37. return wrapper;
  38. }
  39. module.exports = createPartialWrapper;