codegen.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 codegen_exports = {};
  20. __export(codegen_exports, {
  21. escapeWithQuotes: () => escapeWithQuotes,
  22. formatObject: () => formatObject,
  23. quote: () => quote
  24. });
  25. module.exports = __toCommonJS(codegen_exports);
  26. function escapeWithQuotes(text, char = "'") {
  27. const stringified = JSON.stringify(text);
  28. const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
  29. if (char === "'")
  30. return char + escapedText.replace(/[']/g, "\\'") + char;
  31. if (char === '"')
  32. return char + escapedText.replace(/["]/g, '\\"') + char;
  33. if (char === "`")
  34. return char + escapedText.replace(/[`]/g, "\\`") + char;
  35. throw new Error("Invalid escape char");
  36. }
  37. function quote(text) {
  38. return escapeWithQuotes(text, "'");
  39. }
  40. function formatObject(value, indent = " ", mode = "multiline") {
  41. if (typeof value === "string")
  42. return quote(value);
  43. if (Array.isArray(value))
  44. return `[${value.map((o) => formatObject(o)).join(", ")}]`;
  45. if (typeof value === "object") {
  46. const keys = Object.keys(value).filter((key) => value[key] !== void 0).sort();
  47. if (!keys.length)
  48. return "{}";
  49. const tokens = [];
  50. for (const key of keys)
  51. tokens.push(`${key}: ${formatObject(value[key])}`);
  52. if (mode === "multiline")
  53. return `{
  54. ${tokens.join(`,
  55. ${indent}`)}
  56. }`;
  57. return `{ ${tokens.join(", ")} }`;
  58. }
  59. return String(value);
  60. }
  61. // Annotate the CommonJS export names for ESM import in node:
  62. 0 && (module.exports = {
  63. escapeWithQuotes,
  64. formatObject,
  65. quote
  66. });