RegExpHasFlag.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var $RegExpPrototype = GetIntrinsic('%RegExp.prototype%');
  6. var SameValue = require('./SameValue');
  7. var Type = require('./Type');
  8. var $indexOf = callBound('String.prototype.indexOf');
  9. var hasRegExpMatcher = require('is-regex');
  10. var getFlags = require('regexp.prototype.flags');
  11. // https://262.ecma-international.org/13.0/#sec-regexphasflag
  12. module.exports = function RegExpHasFlag(R, codeUnit) {
  13. if (Type(codeUnit) !== 'String' || codeUnit.length !== 1) {
  14. throw new $TypeError('Assertion failed: `string` must be a code unit - a String of length 1');
  15. }
  16. if (Type(R) !== 'Object') {
  17. throw new $TypeError('Assertion failed: Type(R) is not Object');
  18. }
  19. if (!hasRegExpMatcher(R)) { // step 2
  20. if (SameValue(R, $RegExpPrototype)) {
  21. return void undefined; // step 2.a
  22. }
  23. throw new $TypeError('`R` must be a RegExp object'); // step 2.b
  24. }
  25. var flags = getFlags(R); // step 3
  26. return $indexOf(flags, codeUnit) > -1; // steps 4-5
  27. };