diff --git a/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts b/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts index b64bd47..ee6e05d 100644 --- a/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts +++ b/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts @@ -35,6 +35,26 @@ describe("schemaToTypeAliasDeclaration", () => { `); }); + it("should generate enums string (numbers)", () => { + const schema: SchemaObject = { + type: "integer", + enum: ["0", "00", "01", "1", "-1", "-01", "2", "3.33"], + }; + + expect(printSchema(schema)).toMatchInlineSnapshot(` + "export enum Test { + Zero = "0", + "00" = "00", + "01" = "01", + One = "1", + MinusOne = "-1", + "-01" = "-01", + Two = "2", + ThreePointThreeThree = "3.33" + }" + `); + }); + it("should generate a int enum (using big numbers)", () => { const schema: SchemaObject = { type: "string", diff --git a/plugins/typescript/src/core/schemaToEnumDeclaration.ts b/plugins/typescript/src/core/schemaToEnumDeclaration.ts index 3ed256f..e186852 100644 --- a/plugins/typescript/src/core/schemaToEnumDeclaration.ts +++ b/plugins/typescript/src/core/schemaToEnumDeclaration.ts @@ -63,7 +63,11 @@ function getEnumMembers(schema: SchemaObject): ts.EnumMember[] { let enumValueNode: ts.Expression | undefined = undefined; if (typeof enumValue === "string") { - enumName = enumValue; + if (String(Number(enumValue)) === enumValue) { + enumName = convertNumberToWord(Number(enumValue)); + } else { + enumName = enumValue; + } enumValueNode = f.createStringLiteral(enumValue); } else if (typeof enumValue === "number") { enumName = convertNumberToWord(enumValue) diff --git a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts index e884896..726681a 100644 --- a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts +++ b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts @@ -216,6 +216,15 @@ describe("schemaToTypeAliasDeclaration", () => { expect(printSchema(schema)).toBe(`export type Test = 1 | 2 | 3;`); }); + it("should generate enums string (numbers)", () => { + const schema: SchemaObject = { + type: "integer", + enum: ["1", "2", "3"], + }; + + expect(printSchema(schema)).toBe(`export type Test = "1" | "2" | "3";`); + }); + it("should skip example which contains `*/` to avoid confusion", () => { const schema: SchemaObject = { title: "CronTimingCreate", diff --git a/plugins/typescript/src/utils/getEnumProperties.ts b/plugins/typescript/src/utils/getEnumProperties.ts index e5c2f2d..88dfd2e 100644 --- a/plugins/typescript/src/utils/getEnumProperties.ts +++ b/plugins/typescript/src/utils/getEnumProperties.ts @@ -93,6 +93,22 @@ const tens: string[] = [ ]; export const convertNumberToWord = (n: number): string => { + if (n < 0) { + return "minus " + convertNumberToWord(-n); + } + + if (!Number.isInteger(n)) { + const [intPart, fracPart] = n.toString().split("."); + return ( + convertNumberToWord(Number(intPart)) + + " point " + + fracPart + .split("") + .map((digit) => ones[Number(digit)]) + .join(" ") + ); + } + if (n < 20) { return ones[n]; }