diff --git a/src/util/goop.ts b/src/util/goop.ts index 9a219fd..ac9f34e 100644 --- a/src/util/goop.ts +++ b/src/util/goop.ts @@ -4,24 +4,29 @@ * This is just cause I gotta stick to not touching Public templates */ +// keys whose Go field name can't be derived from separators alone +type Special = { id: "ID"; flatrate: "FlatRate" }; + type Camel = S extends "id" ? "ID" : S extends `${infer H}.${infer T}` ? `${H}${Capitalize>}` : S extends `${infer H}_${infer T}` ? `${H}${Capitalize>}` : S; -type Pascal = S extends "id" ? "ID" : Capitalize>; +type Pascal = + S extends keyof Special ? Special[S] : Capitalize>; export type Camelize = T extends readonly (infer U)[] ? Camelize[] : T extends object ? { [K in keyof T as Pascal]: Camelize } : T; +const special: Record = { id: "ID", flatrate: "FlatRate" }; + const pascalKey = (k: string) => - k === "id" - ? "ID" - : k - .replace(/[._](\w)/g, (_, c) => c.toUpperCase()) - .replace(/^\w/, (c) => c.toUpperCase()); + special[k] ?? + k + .replace(/[._](\w)/g, (_, c) => c.toUpperCase()) + .replace(/^\w/, (c) => c.toUpperCase()); export default function ungoop(obj: T): Camelize { return (