array.cjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var array_exports = {};
  20. __export(array_exports, {
  21. makePgArray: () => makePgArray,
  22. parsePgArray: () => parsePgArray,
  23. parsePgNestedArray: () => parsePgNestedArray
  24. });
  25. module.exports = __toCommonJS(array_exports);
  26. function parsePgArrayValue(arrayString, startFrom, inQuotes) {
  27. for (let i = startFrom; i < arrayString.length; i++) {
  28. const char = arrayString[i];
  29. if (char === "\\") {
  30. i++;
  31. continue;
  32. }
  33. if (char === '"') {
  34. return [arrayString.slice(startFrom, i).replace(/\\/g, ""), i + 1];
  35. }
  36. if (inQuotes) {
  37. continue;
  38. }
  39. if (char === "," || char === "}") {
  40. return [arrayString.slice(startFrom, i).replace(/\\/g, ""), i];
  41. }
  42. }
  43. return [arrayString.slice(startFrom).replace(/\\/g, ""), arrayString.length];
  44. }
  45. function parsePgNestedArray(arrayString, startFrom = 0) {
  46. const result = [];
  47. let i = startFrom;
  48. let lastCharIsComma = false;
  49. while (i < arrayString.length) {
  50. const char = arrayString[i];
  51. if (char === ",") {
  52. if (lastCharIsComma || i === startFrom) {
  53. result.push("");
  54. }
  55. lastCharIsComma = true;
  56. i++;
  57. continue;
  58. }
  59. lastCharIsComma = false;
  60. if (char === "\\") {
  61. i += 2;
  62. continue;
  63. }
  64. if (char === '"') {
  65. const [value2, startFrom2] = parsePgArrayValue(arrayString, i + 1, true);
  66. result.push(value2);
  67. i = startFrom2;
  68. continue;
  69. }
  70. if (char === "}") {
  71. return [result, i + 1];
  72. }
  73. if (char === "{") {
  74. const [value2, startFrom2] = parsePgNestedArray(arrayString, i + 1);
  75. result.push(value2);
  76. i = startFrom2;
  77. continue;
  78. }
  79. const [value, newStartFrom] = parsePgArrayValue(arrayString, i, false);
  80. result.push(value);
  81. i = newStartFrom;
  82. }
  83. return [result, i];
  84. }
  85. function parsePgArray(arrayString) {
  86. const [result] = parsePgNestedArray(arrayString, 1);
  87. return result;
  88. }
  89. function makePgArray(array) {
  90. return `{${array.map((item) => {
  91. if (Array.isArray(item)) {
  92. return makePgArray(item);
  93. }
  94. if (typeof item === "string") {
  95. return `"${item.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
  96. }
  97. return `${item}`;
  98. }).join(",")}}`;
  99. }
  100. // Annotate the CommonJS export names for ESM import in node:
  101. 0 && (module.exports = {
  102. makePgArray,
  103. parsePgArray,
  104. parsePgNestedArray
  105. });
  106. //# sourceMappingURL=array.cjs.map