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.

93 lines
3.2 KiB

2 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "splitAtTopLevelOnly", {
  6. enumerable: true,
  7. get: ()=>splitAtTopLevelOnly
  8. });
  9. const _regex = /*#__PURE__*/ _interopRequireWildcard(require("../lib/regex"));
  10. function _getRequireWildcardCache(nodeInterop) {
  11. if (typeof WeakMap !== "function") return null;
  12. var cacheBabelInterop = new WeakMap();
  13. var cacheNodeInterop = new WeakMap();
  14. return (_getRequireWildcardCache = function(nodeInterop) {
  15. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  16. })(nodeInterop);
  17. }
  18. function _interopRequireWildcard(obj, nodeInterop) {
  19. if (!nodeInterop && obj && obj.__esModule) {
  20. return obj;
  21. }
  22. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  23. return {
  24. default: obj
  25. };
  26. }
  27. var cache = _getRequireWildcardCache(nodeInterop);
  28. if (cache && cache.has(obj)) {
  29. return cache.get(obj);
  30. }
  31. var newObj = {};
  32. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  33. for(var key in obj){
  34. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  35. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  36. if (desc && (desc.get || desc.set)) {
  37. Object.defineProperty(newObj, key, desc);
  38. } else {
  39. newObj[key] = obj[key];
  40. }
  41. }
  42. }
  43. newObj.default = obj;
  44. if (cache) {
  45. cache.set(obj, newObj);
  46. }
  47. return newObj;
  48. }
  49. function* splitAtTopLevelOnly(input, separator) {
  50. let SPECIALS = new RegExp(`[(){}\\[\\]${_regex.escape(separator)}]`, "g");
  51. let depth = 0;
  52. let lastIndex = 0;
  53. let found = false;
  54. let separatorIndex = 0;
  55. let separatorStart = 0;
  56. let separatorLength = separator.length;
  57. // Find all paren-like things & character
  58. // And only split on commas if they're top-level
  59. for (let match of input.matchAll(SPECIALS)){
  60. let matchesSeparator = match[0] === separator[separatorIndex];
  61. let atEndOfSeparator = separatorIndex === separatorLength - 1;
  62. let matchesFullSeparator = matchesSeparator && atEndOfSeparator;
  63. if (match[0] === "(") depth++;
  64. if (match[0] === ")") depth--;
  65. if (match[0] === "[") depth++;
  66. if (match[0] === "]") depth--;
  67. if (match[0] === "{") depth++;
  68. if (match[0] === "}") depth--;
  69. if (matchesSeparator && depth === 0) {
  70. if (separatorStart === 0) {
  71. separatorStart = match.index;
  72. }
  73. separatorIndex++;
  74. }
  75. if (matchesFullSeparator && depth === 0) {
  76. found = true;
  77. yield input.substring(lastIndex, separatorStart);
  78. lastIndex = separatorStart + separatorLength;
  79. }
  80. if (separatorIndex === separatorLength) {
  81. separatorIndex = 0;
  82. separatorStart = 0;
  83. }
  84. }
  85. // Provide the last segment of the string if available
  86. // Otherwise the whole string since no `char`s were found
  87. // This mirrors the behavior of string.split()
  88. if (found) {
  89. yield input.substring(lastIndex);
  90. } else {
  91. yield input;
  92. }
  93. }