es.regexp.test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. // TODO: Remove from `core-js@4` since it's moved to entry points
  3. require('../modules/es.regexp.exec');
  4. var $ = require('../internals/export');
  5. var global = require('../internals/global');
  6. var call = require('../internals/function-call');
  7. var uncurryThis = require('../internals/function-uncurry-this');
  8. var isCallable = require('../internals/is-callable');
  9. var isObject = require('../internals/is-object');
  10. var DELEGATES_TO_EXEC = function () {
  11. var execCalled = false;
  12. var re = /[ac]/;
  13. re.exec = function () {
  14. execCalled = true;
  15. return /./.exec.apply(this, arguments);
  16. };
  17. return re.test('abc') === true && execCalled;
  18. }();
  19. var Error = global.Error;
  20. var un$Test = uncurryThis(/./.test);
  21. // `RegExp.prototype.test` method
  22. // https://tc39.es/ecma262/#sec-regexp.prototype.test
  23. $({ target: 'RegExp', proto: true, forced: !DELEGATES_TO_EXEC }, {
  24. test: function (str) {
  25. var exec = this.exec;
  26. if (!isCallable(exec)) return un$Test(this, str);
  27. var result = call(exec, this, str);
  28. if (result !== null && !isObject(result)) {
  29. throw new Error('RegExp exec method returned something other than an Object or null');
  30. }
  31. return !!result;
  32. }
  33. });