es.string.split.js 6.1 KB

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