es.string.split.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
  3. var isRegExp = require('../internals/is-regexp');
  4. var anObject = require('../internals/an-object');
  5. var requireObjectCoercible = require('../internals/require-object-coercible');
  6. var speciesConstructor = require('../internals/species-constructor');
  7. var advanceStringIndex = require('../internals/advance-string-index');
  8. var toLength = require('../internals/to-length');
  9. var callRegExpExec = require('../internals/regexp-exec-abstract');
  10. var regexpExec = require('../internals/regexp-exec');
  11. var stickyHelpers = require('../internals/regexp-sticky-helpers');
  12. var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
  13. var arrayPush = [].push;
  14. var min = Math.min;
  15. var MAX_UINT32 = 0xFFFFFFFF;
  16. // @@split logic
  17. fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
  18. var internalSplit;
  19. if (
  20. 'abbc'.split(/(b)*/)[1] == 'c' ||
  21. // eslint-disable-next-line regexp/no-empty-group -- required for testing
  22. 'test'.split(/(?:)/, -1).length != 4 ||
  23. 'ab'.split(/(?:ab)*/).length != 2 ||
  24. '.'.split(/(.?)(.?)/).length != 4 ||
  25. // eslint-disable-next-line regexp/no-assertion-capturing-group, regexp/no-empty-group -- required for testing
  26. '.'.split(/()()/).length > 1 ||
  27. ''.split(/.?/).length
  28. ) {
  29. // based on es5-shim implementation, need to rework it
  30. internalSplit = function (separator, limit) {
  31. var string = String(requireObjectCoercible(this));
  32. var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
  33. if (lim === 0) return [];
  34. if (separator === undefined) return [string];
  35. // If `separator` is not a regex, use native split
  36. if (!isRegExp(separator)) {
  37. return nativeSplit.call(string, separator, lim);
  38. }
  39. var output = [];
  40. var flags = (separator.ignoreCase ? 'i' : '') +
  41. (separator.multiline ? 'm' : '') +
  42. (separator.unicode ? 'u' : '') +
  43. (separator.sticky ? 'y' : '');
  44. var lastLastIndex = 0;
  45. // Make `global` and avoid `lastIndex` issues by working with a copy
  46. var separatorCopy = new RegExp(separator.source, flags + 'g');
  47. var match, lastIndex, lastLength;
  48. while (match = regexpExec.call(separatorCopy, string)) {
  49. lastIndex = separatorCopy.lastIndex;
  50. if (lastIndex > lastLastIndex) {
  51. output.push(string.slice(lastLastIndex, match.index));
  52. if (match.length > 1 && match.index < string.length) arrayPush.apply(output, match.slice(1));
  53. lastLength = match[0].length;
  54. lastLastIndex = lastIndex;
  55. if (output.length >= lim) break;
  56. }
  57. if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++; // Avoid an infinite loop
  58. }
  59. if (lastLastIndex === string.length) {
  60. if (lastLength || !separatorCopy.test('')) output.push('');
  61. } else output.push(string.slice(lastLastIndex));
  62. return output.length > lim ? output.slice(0, lim) : output;
  63. };
  64. // Chakra, V8
  65. } else if ('0'.split(undefined, 0).length) {
  66. internalSplit = function (separator, limit) {
  67. return separator === undefined && limit === 0 ? [] : nativeSplit.call(this, separator, limit);
  68. };
  69. } else internalSplit = nativeSplit;
  70. return [
  71. // `String.prototype.split` method
  72. // https://tc39.es/ecma262/#sec-string.prototype.split
  73. function split(separator, limit) {
  74. var O = requireObjectCoercible(this);
  75. var splitter = separator == undefined ? undefined : separator[SPLIT];
  76. return splitter !== undefined
  77. ? splitter.call(separator, O, limit)
  78. : internalSplit.call(String(O), separator, limit);
  79. },
  80. // `RegExp.prototype[@@split]` method
  81. // https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  82. //
  83. // NOTE: This cannot be properly polyfilled in engines that don't support
  84. // the 'y' flag.
  85. function (regexp, limit) {
  86. var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
  87. if (res.done) return res.value;
  88. var rx = anObject(regexp);
  89. var S = String(this);
  90. var C = speciesConstructor(rx, RegExp);
  91. var unicodeMatching = rx.unicode;
  92. var flags = (rx.ignoreCase ? 'i' : '') +
  93. (rx.multiline ? 'm' : '') +
  94. (rx.unicode ? 'u' : '') +
  95. (UNSUPPORTED_Y ? 'g' : 'y');
  96. // ^(? + rx + ) is needed, in combination with some S slicing, to
  97. // simulate the 'y' flag.
  98. var splitter = new C(UNSUPPORTED_Y ? '^(?:' + rx.source + ')' : rx, flags);
  99. var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
  100. if (lim === 0) return [];
  101. if (S.length === 0) return callRegExpExec(splitter, S) === null ? [S] : [];
  102. var p = 0;
  103. var q = 0;
  104. var A = [];
  105. while (q < S.length) {
  106. splitter.lastIndex = UNSUPPORTED_Y ? 0 : q;
  107. var z = callRegExpExec(splitter, UNSUPPORTED_Y ? S.slice(q) : S);
  108. var e;
  109. if (
  110. z === null ||
  111. (e = min(toLength(splitter.lastIndex + (UNSUPPORTED_Y ? q : 0)), S.length)) === p
  112. ) {
  113. q = advanceStringIndex(S, q, unicodeMatching);
  114. } else {
  115. A.push(S.slice(p, q));
  116. if (A.length === lim) return A;
  117. for (var i = 1; i <= z.length - 1; i++) {
  118. A.push(z[i]);
  119. if (A.length === lim) return A;
  120. }
  121. q = p = e;
  122. }
  123. }
  124. A.push(S.slice(p));
  125. return A;
  126. }
  127. ];
  128. }, UNSUPPORTED_Y);