1'use strict'; 2 3var isRegExp = require('./_is-regexp'); 4var anObject = require('./_an-object'); 5var speciesConstructor = require('./_species-constructor'); 6var advanceStringIndex = require('./_advance-string-index'); 7var toLength = require('./_to-length'); 8var callRegExpExec = require('./_regexp-exec-abstract'); 9var regexpExec = require('./_regexp-exec'); 10var fails = require('./_fails'); 11var $min = Math.min; 12var $push = [].push; 13var $SPLIT = 'split'; 14var LENGTH = 'length'; 15var LAST_INDEX = 'lastIndex'; 16var MAX_UINT32 = 0xffffffff; 17 18// babel-minify transpiles RegExp('x', 'y') -> /x/y and it causes SyntaxError 19var SUPPORTS_Y = !fails(function () { RegExp(MAX_UINT32, 'y'); }); 20 21// @@split logic 22require('./_fix-re-wks')('split', 2, function (defined, SPLIT, $split, maybeCallNative) { 23 var internalSplit; 24 if ( 25 'abbc'[$SPLIT](/(b)*/)[1] == 'c' || 26 'test'[$SPLIT](/(?:)/, -1)[LENGTH] != 4 || 27 'ab'[$SPLIT](/(?:ab)*/)[LENGTH] != 2 || 28 '.'[$SPLIT](/(.?)(.?)/)[LENGTH] != 4 || 29 '.'[$SPLIT](/()()/)[LENGTH] > 1 || 30 ''[$SPLIT](/.?/)[LENGTH] 31 ) { 32 // based on es5-shim implementation, need to rework it 33 internalSplit = function (separator, limit) { 34 var string = String(this); 35 if (separator === undefined && limit === 0) return []; 36 // If `separator` is not a regex, use native split 37 if (!isRegExp(separator)) return $split.call(string, separator, limit); 38 var output = []; 39 var flags = (separator.ignoreCase ? 'i' : '') + 40 (separator.multiline ? 'm' : '') + 41 (separator.unicode ? 'u' : '') + 42 (separator.sticky ? 'y' : ''); 43 var lastLastIndex = 0; 44 var splitLimit = limit === undefined ? MAX_UINT32 : limit >>> 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[LAST_INDEX]; 50 if (lastIndex > lastLastIndex) { 51 output.push(string.slice(lastLastIndex, match.index)); 52 if (match[LENGTH] > 1 && match.index < string[LENGTH]) $push.apply(output, match.slice(1)); 53 lastLength = match[0][LENGTH]; 54 lastLastIndex = lastIndex; 55 if (output[LENGTH] >= splitLimit) break; 56 } 57 if (separatorCopy[LAST_INDEX] === match.index) separatorCopy[LAST_INDEX]++; // 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] > splitLimit ? output.slice(0, splitLimit) : output; 63 }; 64 // Chakra, V8 65 } else if ('0'[$SPLIT](undefined, 0)[LENGTH]) { 66 internalSplit = function (separator, limit) { 67 return separator === undefined && limit === 0 ? [] : $split.call(this, separator, limit); 68 }; 69 } else { 70 internalSplit = $split; 71 } 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 = defined(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 !== $split); 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}); 135