2016-10-10 03:43:44 +02:00
|
|
|
// @@split logic
|
2017-08-14 05:01:11 +02:00
|
|
|
require('./_fix-re-wks')('split', 2, function (defined, SPLIT, $split) {
|
2017-05-27 17:36:13 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
var isRegExp = require('./_is-regexp');
|
|
|
|
var _split = $split;
|
|
|
|
var $push = [].push;
|
|
|
|
var $SPLIT = 'split';
|
|
|
|
var LENGTH = 'length';
|
|
|
|
var LAST_INDEX = 'lastIndex';
|
|
|
|
if (
|
2017-05-27 17:36:13 +02:00
|
|
|
'abbc'[$SPLIT](/(b)*/)[1] == 'c' ||
|
|
|
|
'test'[$SPLIT](/(?:)/, -1)[LENGTH] != 4 ||
|
|
|
|
'ab'[$SPLIT](/(?:ab)*/)[LENGTH] != 2 ||
|
|
|
|
'.'[$SPLIT](/(.?)(.?)/)[LENGTH] != 4 ||
|
|
|
|
'.'[$SPLIT](/()()/)[LENGTH] > 1 ||
|
|
|
|
''[$SPLIT](/.?/)[LENGTH]
|
2017-08-14 05:01:11 +02:00
|
|
|
) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var NPCG = /()??/.exec('')[1] === undefined; // nonparticipating capturing group
|
|
|
|
// based on es5-shim implementation, need to rework it
|
2017-08-14 05:01:11 +02:00
|
|
|
$split = function (separator, limit) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var string = String(this);
|
2017-08-14 05:01:11 +02:00
|
|
|
if (separator === undefined && limit === 0) return [];
|
2017-05-27 17:36:13 +02:00
|
|
|
// If `separator` is not a regex, use native split
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!isRegExp(separator)) return _split.call(string, separator, limit);
|
2017-05-27 17:36:13 +02:00
|
|
|
var output = [];
|
|
|
|
var flags = (separator.ignoreCase ? 'i' : '') +
|
|
|
|
(separator.multiline ? 'm' : '') +
|
|
|
|
(separator.unicode ? 'u' : '') +
|
|
|
|
(separator.sticky ? 'y' : '');
|
|
|
|
var lastLastIndex = 0;
|
|
|
|
var splitLimit = limit === undefined ? 4294967295 : limit >>> 0;
|
|
|
|
// Make `global` and avoid `lastIndex` issues by working with a copy
|
|
|
|
var separatorCopy = new RegExp(separator.source, flags + 'g');
|
|
|
|
var separator2, match, lastIndex, lastLength, i;
|
|
|
|
// Doesn't need flags gy, but they don't hurt
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!NPCG) separator2 = new RegExp('^' + separatorCopy.source + '$(?!\\s)', flags);
|
|
|
|
while (match = separatorCopy.exec(string)) {
|
2017-05-27 17:36:13 +02:00
|
|
|
// `separatorCopy.lastIndex` is not reliable cross-browser
|
|
|
|
lastIndex = match.index + match[0][LENGTH];
|
2017-08-14 05:01:11 +02:00
|
|
|
if (lastIndex > lastLastIndex) {
|
2017-05-27 17:36:13 +02:00
|
|
|
output.push(string.slice(lastLastIndex, match.index));
|
|
|
|
// Fix browsers whose `exec` methods don't consistently return `undefined` for NPCG
|
2017-08-14 05:01:11 +02:00
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
if (!NPCG && match[LENGTH] > 1) match[0].replace(separator2, function () {
|
|
|
|
for (i = 1; i < arguments[LENGTH] - 2; i++) if (arguments[i] === undefined) match[i] = undefined;
|
2017-05-27 17:36:13 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
if (match[LENGTH] > 1 && match.index < string[LENGTH]) $push.apply(output, match.slice(1));
|
2017-05-27 17:36:13 +02:00
|
|
|
lastLength = match[0][LENGTH];
|
|
|
|
lastLastIndex = lastIndex;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (output[LENGTH] >= splitLimit) break;
|
2017-05-27 17:36:13 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (separatorCopy[LAST_INDEX] === match.index) separatorCopy[LAST_INDEX]++; // Avoid an infinite loop
|
2017-05-27 17:36:13 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (lastLastIndex === string[LENGTH]) {
|
|
|
|
if (lastLength || !separatorCopy.test('')) output.push('');
|
2017-05-27 17:36:13 +02:00
|
|
|
} else output.push(string.slice(lastLastIndex));
|
|
|
|
return output[LENGTH] > splitLimit ? output.slice(0, splitLimit) : output;
|
|
|
|
};
|
|
|
|
// Chakra, V8
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if ('0'[$SPLIT](undefined, 0)[LENGTH]) {
|
|
|
|
$split = function (separator, limit) {
|
2017-05-27 17:36:13 +02:00
|
|
|
return separator === undefined && limit === 0 ? [] : _split.call(this, separator, limit);
|
|
|
|
};
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
// 21.1.3.17 String.prototype.split(separator, limit)
|
2017-08-14 05:01:11 +02:00
|
|
|
return [function split(separator, limit) {
|
|
|
|
var O = defined(this);
|
|
|
|
var fn = separator == undefined ? undefined : separator[SPLIT];
|
2017-05-27 17:36:13 +02:00
|
|
|
return fn !== undefined ? fn.call(separator, O, limit) : $split.call(String(O), separator, limit);
|
|
|
|
}, $split];
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|