Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

es.string.split.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
  5. var anObject = require('../internals/an-object');
  6. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  7. var requireObjectCoercible = require('../internals/require-object-coercible');
  8. var speciesConstructor = require('../internals/species-constructor');
  9. var advanceStringIndex = require('../internals/advance-string-index');
  10. var toLength = require('../internals/to-length');
  11. var toString = require('../internals/to-string');
  12. var getMethod = require('../internals/get-method');
  13. var regExpExec = require('../internals/regexp-exec-abstract');
  14. var stickyHelpers = require('../internals/regexp-sticky-helpers');
  15. var fails = require('../internals/fails');
  16. var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
  17. var MAX_UINT32 = 0xFFFFFFFF;
  18. var min = Math.min;
  19. var push = uncurryThis([].push);
  20. var stringSlice = uncurryThis(''.slice);
  21. // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
  22. // Weex JS has frozen built-in prototypes, so use try / catch wrapper
  23. var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
  24. // eslint-disable-next-line regexp/no-empty-group -- required for testing
  25. var re = /(?:)/;
  26. var originalExec = re.exec;
  27. re.exec = function () { return originalExec.apply(this, arguments); };
  28. var result = 'ab'.split(re);
  29. return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
  30. });
  31. var BUGGY = 'abbc'.split(/(b)*/)[1] === 'c' ||
  32. // eslint-disable-next-line regexp/no-empty-group -- required for testing
  33. 'test'.split(/(?:)/, -1).length !== 4 ||
  34. 'ab'.split(/(?:ab)*/).length !== 2 ||
  35. '.'.split(/(.?)(.?)/).length !== 4 ||
  36. // eslint-disable-next-line regexp/no-empty-capturing-group, regexp/no-empty-group -- required for testing
  37. '.'.split(/()()/).length > 1 ||
  38. ''.split(/.?/).length;
  39. // @@split logic
  40. fixRegExpWellKnownSymbolLogic('split', function (SPLIT, nativeSplit, maybeCallNative) {
  41. var internalSplit = '0'.split(undefined, 0).length ? function (separator, limit) {
  42. return separator === undefined && limit === 0 ? [] : call(nativeSplit, this, separator, limit);
  43. } : nativeSplit;
  44. return [
  45. // `String.prototype.split` method
  46. // https://tc39.es/ecma262/#sec-string.prototype.split
  47. function split(separator, limit) {
  48. var O = requireObjectCoercible(this);
  49. var splitter = isNullOrUndefined(separator) ? undefined : getMethod(separator, SPLIT);
  50. return splitter
  51. ? call(splitter, separator, O, limit)
  52. : call(internalSplit, toString(O), separator, limit);
  53. },
  54. // `RegExp.prototype[@@split]` method
  55. // https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  56. //
  57. // NOTE: This cannot be properly polyfilled in engines that don't support
  58. // the 'y' flag.
  59. function (string, limit) {
  60. var rx = anObject(this);
  61. var S = toString(string);
  62. if (!BUGGY) {
  63. var res = maybeCallNative(internalSplit, rx, S, limit, internalSplit !== nativeSplit);
  64. if (res.done) return res.value;
  65. }
  66. var C = speciesConstructor(rx, RegExp);
  67. var unicodeMatching = rx.unicode;
  68. var flags = (rx.ignoreCase ? 'i' : '') +
  69. (rx.multiline ? 'm' : '') +
  70. (rx.unicode ? 'u' : '') +
  71. (UNSUPPORTED_Y ? 'g' : 'y');
  72. // ^(? + rx + ) is needed, in combination with some S slicing, to
  73. // simulate the 'y' flag.
  74. var splitter = new C(UNSUPPORTED_Y ? '^(?:' + rx.source + ')' : rx, flags);
  75. var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
  76. if (lim === 0) return [];
  77. if (S.length === 0) return regExpExec(splitter, S) === null ? [S] : [];
  78. var p = 0;
  79. var q = 0;
  80. var A = [];
  81. while (q < S.length) {
  82. splitter.lastIndex = UNSUPPORTED_Y ? 0 : q;
  83. var z = regExpExec(splitter, UNSUPPORTED_Y ? stringSlice(S, q) : S);
  84. var e;
  85. if (
  86. z === null ||
  87. (e = min(toLength(splitter.lastIndex + (UNSUPPORTED_Y ? q : 0)), S.length)) === p
  88. ) {
  89. q = advanceStringIndex(S, q, unicodeMatching);
  90. } else {
  91. push(A, stringSlice(S, p, q));
  92. if (A.length === lim) return A;
  93. for (var i = 1; i <= z.length - 1; i++) {
  94. push(A, z[i]);
  95. if (A.length === lim) return A;
  96. }
  97. q = p = e;
  98. }
  99. }
  100. push(A, stringSlice(S, p));
  101. return A;
  102. }
  103. ];
  104. }, BUGGY || !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC, UNSUPPORTED_Y);