index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var bind = require('function-bind');
  3. var GetIntrinsic = require('get-intrinsic');
  4. var setFunctionLength = require('set-function-length');
  5. var $TypeError = require('es-errors/type');
  6. var $apply = GetIntrinsic('%Function.prototype.apply%');
  7. var $call = GetIntrinsic('%Function.prototype.call%');
  8. var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply);
  9. var $defineProperty = require('es-define-property');
  10. var $max = GetIntrinsic('%Math.max%');
  11. module.exports = function callBind(originalFunction) {
  12. if (typeof originalFunction !== 'function') {
  13. throw new $TypeError('a function is required');
  14. }
  15. var func = $reflectApply(bind, $call, arguments);
  16. return setFunctionLength(
  17. func,
  18. 1 + $max(0, originalFunction.length - (arguments.length - 1)),
  19. true
  20. );
  21. };
  22. var applyBind = function applyBind() {
  23. return $reflectApply(bind, $apply, arguments);
  24. };
  25. if ($defineProperty) {
  26. $defineProperty(module.exports, 'apply', { value: applyBind });
  27. } else {
  28. module.exports.apply = applyBind;
  29. }