es6.regexp.split.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // @@split logic
  2. require('./_fix-re-wks')('split', 2, function (defined, SPLIT, $split) {
  3. 'use strict';
  4. var isRegExp = require('./_is-regexp');
  5. var _split = $split;
  6. var $push = [].push;
  7. var $SPLIT = 'split';
  8. var LENGTH = 'length';
  9. var LAST_INDEX = 'lastIndex';
  10. if (
  11. 'abbc'[$SPLIT](/(b)*/)[1] == 'c' ||
  12. 'test'[$SPLIT](/(?:)/, -1)[LENGTH] != 4 ||
  13. 'ab'[$SPLIT](/(?:ab)*/)[LENGTH] != 2 ||
  14. '.'[$SPLIT](/(.?)(.?)/)[LENGTH] != 4 ||
  15. '.'[$SPLIT](/()()/)[LENGTH] > 1 ||
  16. ''[$SPLIT](/.?/)[LENGTH]
  17. ) {
  18. var NPCG = /()??/.exec('')[1] === undefined; // nonparticipating capturing group
  19. // based on es5-shim implementation, need to rework it
  20. $split = function (separator, limit) {
  21. var string = String(this);
  22. if (separator === undefined && limit === 0) return [];
  23. // If `separator` is not a regex, use native split
  24. if (!isRegExp(separator)) return _split.call(string, separator, limit);
  25. var output = [];
  26. var flags = (separator.ignoreCase ? 'i' : '') +
  27. (separator.multiline ? 'm' : '') +
  28. (separator.unicode ? 'u' : '') +
  29. (separator.sticky ? 'y' : '');
  30. var lastLastIndex = 0;
  31. var splitLimit = limit === undefined ? 4294967295 : limit >>> 0;
  32. // Make `global` and avoid `lastIndex` issues by working with a copy
  33. var separatorCopy = new RegExp(separator.source, flags + 'g');
  34. var separator2, match, lastIndex, lastLength, i;
  35. // Doesn't need flags gy, but they don't hurt
  36. if (!NPCG) separator2 = new RegExp('^' + separatorCopy.source + '$(?!\\s)', flags);
  37. while (match = separatorCopy.exec(string)) {
  38. // `separatorCopy.lastIndex` is not reliable cross-browser
  39. lastIndex = match.index + match[0][LENGTH];
  40. if (lastIndex > lastLastIndex) {
  41. output.push(string.slice(lastLastIndex, match.index));
  42. // Fix browsers whose `exec` methods don't consistently return `undefined` for NPCG
  43. // eslint-disable-next-line no-loop-func
  44. if (!NPCG && match[LENGTH] > 1) match[0].replace(separator2, function () {
  45. for (i = 1; i < arguments[LENGTH] - 2; i++) if (arguments[i] === undefined) match[i] = undefined;
  46. });
  47. if (match[LENGTH] > 1 && match.index < string[LENGTH]) $push.apply(output, match.slice(1));
  48. lastLength = match[0][LENGTH];
  49. lastLastIndex = lastIndex;
  50. if (output[LENGTH] >= splitLimit) break;
  51. }
  52. if (separatorCopy[LAST_INDEX] === match.index) separatorCopy[LAST_INDEX]++; // Avoid an infinite loop
  53. }
  54. if (lastLastIndex === string[LENGTH]) {
  55. if (lastLength || !separatorCopy.test('')) output.push('');
  56. } else output.push(string.slice(lastLastIndex));
  57. return output[LENGTH] > splitLimit ? output.slice(0, splitLimit) : output;
  58. };
  59. // Chakra, V8
  60. } else if ('0'[$SPLIT](undefined, 0)[LENGTH]) {
  61. $split = function (separator, limit) {
  62. return separator === undefined && limit === 0 ? [] : _split.call(this, separator, limit);
  63. };
  64. }
  65. // 21.1.3.17 String.prototype.split(separator, limit)
  66. return [function split(separator, limit) {
  67. var O = defined(this);
  68. var fn = separator == undefined ? undefined : separator[SPLIT];
  69. return fn !== undefined ? fn.call(separator, O, limit) : $split.call(String(O), separator, limit);
  70. }, $split];
  71. });