es.string.match-all.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. /* eslint-disable es-x/no-string-prototype-matchall -- safe */
  3. var $ = require('../internals/export');
  4. var global = require('../internals/global');
  5. var call = require('../internals/function-call');
  6. var uncurryThis = require('../internals/function-uncurry-this');
  7. var createIteratorConstructor = require('../internals/create-iterator-constructor');
  8. var requireObjectCoercible = require('../internals/require-object-coercible');
  9. var toLength = require('../internals/to-length');
  10. var toString = require('../internals/to-string');
  11. var anObject = require('../internals/an-object');
  12. var classof = require('../internals/classof-raw');
  13. var isPrototypeOf = require('../internals/object-is-prototype-of');
  14. var isRegExp = require('../internals/is-regexp');
  15. var regExpFlags = require('../internals/regexp-flags');
  16. var getMethod = require('../internals/get-method');
  17. var redefine = require('../internals/redefine');
  18. var fails = require('../internals/fails');
  19. var wellKnownSymbol = require('../internals/well-known-symbol');
  20. var speciesConstructor = require('../internals/species-constructor');
  21. var advanceStringIndex = require('../internals/advance-string-index');
  22. var regExpExec = require('../internals/regexp-exec-abstract');
  23. var InternalStateModule = require('../internals/internal-state');
  24. var IS_PURE = require('../internals/is-pure');
  25. var MATCH_ALL = wellKnownSymbol('matchAll');
  26. var REGEXP_STRING = 'RegExp String';
  27. var REGEXP_STRING_ITERATOR = REGEXP_STRING + ' Iterator';
  28. var setInternalState = InternalStateModule.set;
  29. var getInternalState = InternalStateModule.getterFor(REGEXP_STRING_ITERATOR);
  30. var RegExpPrototype = RegExp.prototype;
  31. var TypeError = global.TypeError;
  32. var getFlags = uncurryThis(regExpFlags);
  33. var stringIndexOf = uncurryThis(''.indexOf);
  34. var un$MatchAll = uncurryThis(''.matchAll);
  35. var WORKS_WITH_NON_GLOBAL_REGEX = !!un$MatchAll && !fails(function () {
  36. un$MatchAll('a', /./);
  37. });
  38. var $RegExpStringIterator = createIteratorConstructor(function RegExpStringIterator(regexp, string, $global, fullUnicode) {
  39. setInternalState(this, {
  40. type: REGEXP_STRING_ITERATOR,
  41. regexp: regexp,
  42. string: string,
  43. global: $global,
  44. unicode: fullUnicode,
  45. done: false
  46. });
  47. }, REGEXP_STRING, function next() {
  48. var state = getInternalState(this);
  49. if (state.done) return { value: undefined, done: true };
  50. var R = state.regexp;
  51. var S = state.string;
  52. var match = regExpExec(R, S);
  53. if (match === null) return { value: undefined, done: state.done = true };
  54. if (state.global) {
  55. if (toString(match[0]) === '') R.lastIndex = advanceStringIndex(S, toLength(R.lastIndex), state.unicode);
  56. return { value: match, done: false };
  57. }
  58. state.done = true;
  59. return { value: match, done: false };
  60. });
  61. var $matchAll = function (string) {
  62. var R = anObject(this);
  63. var S = toString(string);
  64. var C, flagsValue, flags, matcher, $global, fullUnicode;
  65. C = speciesConstructor(R, RegExp);
  66. flagsValue = R.flags;
  67. if (flagsValue === undefined && isPrototypeOf(RegExpPrototype, R) && !('flags' in RegExpPrototype)) {
  68. flagsValue = getFlags(R);
  69. }
  70. flags = flagsValue === undefined ? '' : toString(flagsValue);
  71. matcher = new C(C === RegExp ? R.source : R, flags);
  72. $global = !!~stringIndexOf(flags, 'g');
  73. fullUnicode = !!~stringIndexOf(flags, 'u');
  74. matcher.lastIndex = toLength(R.lastIndex);
  75. return new $RegExpStringIterator(matcher, S, $global, fullUnicode);
  76. };
  77. // `String.prototype.matchAll` method
  78. // https://tc39.es/ecma262/#sec-string.prototype.matchall
  79. $({ target: 'String', proto: true, forced: WORKS_WITH_NON_GLOBAL_REGEX }, {
  80. matchAll: function matchAll(regexp) {
  81. var O = requireObjectCoercible(this);
  82. var flags, S, matcher, rx;
  83. if (regexp != null) {
  84. if (isRegExp(regexp)) {
  85. flags = toString(requireObjectCoercible('flags' in RegExpPrototype
  86. ? regexp.flags
  87. : getFlags(regexp)
  88. ));
  89. if (!~stringIndexOf(flags, 'g')) throw TypeError('`.matchAll` does not allow non-global regexes');
  90. }
  91. if (WORKS_WITH_NON_GLOBAL_REGEX) return un$MatchAll(O, regexp);
  92. matcher = getMethod(regexp, MATCH_ALL);
  93. if (matcher === undefined && IS_PURE && classof(regexp) == 'RegExp') matcher = $matchAll;
  94. if (matcher) return call(matcher, regexp, O);
  95. } else if (WORKS_WITH_NON_GLOBAL_REGEX) return un$MatchAll(O, regexp);
  96. S = toString(O);
  97. rx = new RegExp(regexp, 'g');
  98. return IS_PURE ? call($matchAll, rx, S) : rx[MATCH_ALL](S);
  99. }
  100. });
  101. IS_PURE || MATCH_ALL in RegExpPrototype || redefine(RegExpPrototype, MATCH_ALL, $matchAll);