UNPKG

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