es.string.match-all.js 4.5 KB

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