typed-openapi
Version:
80 lines (78 loc) • 1.1 kB
text/typescript
const reservedWords = new Set([
// TS keywords and built-ins
"import",
"package",
"namespace",
"Record",
"Partial",
"Required",
"Readonly",
"Pick",
"Omit",
"String",
"Number",
"Boolean",
"Object",
"Array",
"Function",
"any",
"unknown",
"never",
"void",
"extends",
"super",
"class",
"interface",
"type",
"enum",
"const",
"let",
"var",
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"default",
"break",
"continue",
"return",
"try",
"catch",
"finally",
"throw",
"new",
"delete",
"in",
"instanceof",
"typeof",
"void",
"with",
"yield",
"await",
"static",
"public",
"private",
"protected",
"abstract",
"as",
"asserts",
"from",
"get",
"set",
"module",
"require",
"keyof",
"readonly",
"global",
"symbol",
"bigint",
]);
export function sanitizeName(name: string, type: "schema" | "endpoint") {
let n = name.replace(/[\W/]+/g, "_");
if (/^\d/.test(n)) n = "_" + n;
if (reservedWords.has(n)) n = (type === "schema" ? "Schema_" : "Endpoint_") + n;
return n;
}