es.regexp.test.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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 call = require('../internals/function-call');
  6. var isCallable = require('../internals/is-callable');
  7. var anObject = require('../internals/an-object');
  8. var toString = require('../internals/to-string');
  9. var DELEGATES_TO_EXEC = function () {
  10. var execCalled = false;
  11. var re = /[ac]/;
  12. re.exec = function () {
  13. execCalled = true;
  14. return /./.exec.apply(this, arguments);
  15. };
  16. return re.test('abc') === true && execCalled;
  17. }();
  18. var nativeTest = /./.test;
  19. // `RegExp.prototype.test` method
  20. // https://tc39.es/ecma262/#sec-regexp.prototype.test
  21. $({ target: 'RegExp', proto: true, forced: !DELEGATES_TO_EXEC }, {
  22. test: function (S) {
  23. var R = anObject(this);
  24. var string = toString(S);
  25. var exec = R.exec;
  26. if (!isCallable(exec)) return call(nativeTest, R, string);
  27. var result = call(exec, R, string);
  28. if (result === null) return false;
  29. anObject(result);
  30. return true;
  31. }
  32. });