casing.cjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 casing_exports = {};
  20. __export(casing_exports, {
  21. CasingCache: () => CasingCache,
  22. toCamelCase: () => toCamelCase,
  23. toSnakeCase: () => toSnakeCase
  24. });
  25. module.exports = __toCommonJS(casing_exports);
  26. var import_entity = require("./entity.cjs");
  27. var import_table = require("./table.cjs");
  28. function toSnakeCase(input) {
  29. const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
  30. return words.map((word) => word.toLowerCase()).join("_");
  31. }
  32. function toCamelCase(input) {
  33. const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
  34. return words.reduce((acc, word, i) => {
  35. const formattedWord = i === 0 ? word.toLowerCase() : `${word[0].toUpperCase()}${word.slice(1)}`;
  36. return acc + formattedWord;
  37. }, "");
  38. }
  39. function noopCase(input) {
  40. return input;
  41. }
  42. class CasingCache {
  43. static [import_entity.entityKind] = "CasingCache";
  44. /** @internal */
  45. cache = {};
  46. cachedTables = {};
  47. convert;
  48. constructor(casing) {
  49. this.convert = casing === "snake_case" ? toSnakeCase : casing === "camelCase" ? toCamelCase : noopCase;
  50. }
  51. getColumnCasing(column) {
  52. if (!column.keyAsName) return column.name;
  53. const schema = column.table[import_table.Table.Symbol.Schema] ?? "public";
  54. const tableName = column.table[import_table.Table.Symbol.OriginalName];
  55. const key = `${schema}.${tableName}.${column.name}`;
  56. if (!this.cache[key]) {
  57. this.cacheTable(column.table);
  58. }
  59. return this.cache[key];
  60. }
  61. cacheTable(table) {
  62. const schema = table[import_table.Table.Symbol.Schema] ?? "public";
  63. const tableName = table[import_table.Table.Symbol.OriginalName];
  64. const tableKey = `${schema}.${tableName}`;
  65. if (!this.cachedTables[tableKey]) {
  66. for (const column of Object.values(table[import_table.Table.Symbol.Columns])) {
  67. const columnKey = `${tableKey}.${column.name}`;
  68. this.cache[columnKey] = this.convert(column.name);
  69. }
  70. this.cachedTables[tableKey] = true;
  71. }
  72. }
  73. clearCache() {
  74. this.cache = {};
  75. this.cachedTables = {};
  76. }
  77. }
  78. // Annotate the CommonJS export names for ESM import in node:
  79. 0 && (module.exports = {
  80. CasingCache,
  81. toCamelCase,
  82. toSnakeCase
  83. });
  84. //# sourceMappingURL=casing.cjs.map