RegExpExec.js 817 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var regexExec = require('call-bind/callBound')('RegExp.prototype.exec');
  4. var Call = require('./Call');
  5. var Get = require('./Get');
  6. var IsCallable = require('./IsCallable');
  7. var Type = require('./Type');
  8. // https://262.ecma-international.org/6.0/#sec-regexpexec
  9. module.exports = function RegExpExec(R, S) {
  10. if (Type(R) !== 'Object') {
  11. throw new $TypeError('Assertion failed: `R` must be an Object');
  12. }
  13. if (typeof S !== 'string') {
  14. throw new $TypeError('Assertion failed: `S` must be a String');
  15. }
  16. var exec = Get(R, 'exec');
  17. if (IsCallable(exec)) {
  18. var result = Call(exec, R, [S]);
  19. if (typeof result === 'object') {
  20. return result;
  21. }
  22. throw new $TypeError('"exec" method must return `null` or an Object');
  23. }
  24. return regexExec(R, S);
  25. };