easy-nest-generator
Version:
generate interface, hooks for nestjs project
38 lines (32 loc) • 762 B
text/typescript
/**
* Cleans the type text to rename any non-JS primitive or non-String/Number types with an 'I' prefix.
*/
export function cleanType(typeText: string): string {
const jsPrimitives = [
"string",
"number",
"boolean",
"null",
"undefined",
"void",
"date",
"any",
];
if (
jsPrimitives.includes(typeText.toLowerCase()) ||
(typeText.endsWith("[]") &&
jsPrimitives.includes(typeText.replace("[]", "").toLowerCase()))
) {
return typeText;
}
if (typeText.startsWith("Array<")) {
return typeText.replace("Array<", "I").replace(">", "");
}
if (typeText.startsWith("{")) {
return typeText;
}
if (typeText && !typeText.startsWith("I")) {
return `I${typeText}`;
}
return typeText;
}