regexp-matchall.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. // var Construct = require('es-abstract/2020/Construct');
  3. var Get = require('es-abstract/2020/Get');
  4. var Set = require('es-abstract/2020/Set');
  5. var SpeciesConstructor = require('es-abstract/2020/SpeciesConstructor');
  6. var ToLength = require('es-abstract/2020/ToLength');
  7. var ToString = require('es-abstract/2020/ToString');
  8. var Type = require('es-abstract/2020/Type');
  9. var flagsGetter = require('regexp.prototype.flags');
  10. var RegExpStringIterator = require('./helpers/RegExpStringIterator');
  11. var OrigRegExp = RegExp;
  12. var CreateRegExpStringIterator = function CreateRegExpStringIterator(R, S, global, fullUnicode) {
  13. if (Type(S) !== 'String') {
  14. throw new TypeError('"S" value must be a String');
  15. }
  16. if (Type(global) !== 'Boolean') {
  17. throw new TypeError('"global" value must be a Boolean');
  18. }
  19. if (Type(fullUnicode) !== 'Boolean') {
  20. throw new TypeError('"fullUnicode" value must be a Boolean');
  21. }
  22. var iterator = new RegExpStringIterator(R, S, global, fullUnicode);
  23. return iterator;
  24. };
  25. var supportsConstructingWithFlags = 'flags' in RegExp.prototype;
  26. var constructRegexWithFlags = function constructRegex(C, R) {
  27. var matcher;
  28. // workaround for older engines that lack RegExp.prototype.flags
  29. var flags = 'flags' in R ? Get(R, 'flags') : ToString(flagsGetter(R));
  30. if (supportsConstructingWithFlags && typeof flags === 'string') {
  31. matcher = new C(R, flags);
  32. } else if (C === OrigRegExp) {
  33. // workaround for older engines that can not construct a RegExp with flags
  34. matcher = new C(R.source, flags);
  35. } else {
  36. matcher = new C(R, flags);
  37. }
  38. return { flags: flags, matcher: matcher };
  39. };
  40. var regexMatchAll = function SymbolMatchAll(string) {
  41. var R = this;
  42. if (Type(R) !== 'Object') {
  43. throw new TypeError('"this" value must be an Object');
  44. }
  45. var S = ToString(string);
  46. var C = SpeciesConstructor(R, OrigRegExp);
  47. var tmp = constructRegexWithFlags(C, R);
  48. // var flags = ToString(Get(R, 'flags'));
  49. var flags = tmp.flags;
  50. // var matcher = Construct(C, [R, flags]);
  51. var matcher = tmp.matcher;
  52. var lastIndex = ToLength(Get(R, 'lastIndex'));
  53. Set(matcher, 'lastIndex', lastIndex, true);
  54. var global = flags.indexOf('g') > -1;
  55. var fullUnicode = flags.indexOf('u') > -1;
  56. return CreateRegExpStringIterator(matcher, S, global, fullUnicode);
  57. };
  58. var defineP = Object.defineProperty;
  59. var gOPD = Object.getOwnPropertyDescriptor;
  60. if (defineP && gOPD) {
  61. var desc = gOPD(regexMatchAll, 'name');
  62. if (desc && desc.configurable) {
  63. defineP(regexMatchAll, 'name', { value: '[Symbol.matchAll]' });
  64. }
  65. }
  66. module.exports = regexMatchAll;