es.string.match-all.js 4.3 KB

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