es6.regexp.split.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // @@split logic
  2. require('./_fix-re-wks')('split', 2, function(defined, SPLIT, $split){
  3. 'use strict';
  4. var isRegExp = require('./_is-regexp')
  5. , _split = $split
  6. , $push = [].push
  7. , $SPLIT = 'split'
  8. , LENGTH = 'length'
  9. , 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. if(!NPCG && match[LENGTH] > 1)match[0].replace(separator2, function(){
  44. for(i = 1; i < arguments[LENGTH] - 2; i++)if(arguments[i] === undefined)match[i] = undefined;
  45. });
  46. if(match[LENGTH] > 1 && match.index < string[LENGTH])$push.apply(output, match.slice(1));
  47. lastLength = match[0][LENGTH];
  48. lastLastIndex = lastIndex;
  49. if(output[LENGTH] >= splitLimit)break;
  50. }
  51. if(separatorCopy[LAST_INDEX] === match.index)separatorCopy[LAST_INDEX]++; // Avoid an infinite loop
  52. }
  53. if(lastLastIndex === string[LENGTH]){
  54. if(lastLength || !separatorCopy.test(''))output.push('');
  55. } else output.push(string.slice(lastLastIndex));
  56. return output[LENGTH] > splitLimit ? output.slice(0, splitLimit) : output;
  57. };
  58. // Chakra, V8
  59. } else if('0'[$SPLIT](undefined, 0)[LENGTH]){
  60. $split = function(separator, limit){
  61. return separator === undefined && limit === 0 ? [] : _split.call(this, separator, limit);
  62. };
  63. }
  64. // 21.1.3.17 String.prototype.split(separator, limit)
  65. return [function split(separator, limit){
  66. var O = defined(this)
  67. , fn = separator == undefined ? undefined : separator[SPLIT];
  68. return fn !== undefined ? fn.call(separator, O, limit) : $split.call(String(O), separator, limit);
  69. }, $split];
  70. });