diff --git a/src/index.test.ts b/src/index.test.ts index 6a0c377..fb4e867 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -368,6 +368,62 @@ describe('generateJSONSchemaTypes()', () => { }); }); + test('should work with enums having values starting with a number', async () => { + const schema: JSONSchema7 = { + title: 'Limit', + type: 'string', + enum: ['1m', '1d', '1w'], + }; + + expect( + toSource( + await generateJSONSchemaTypes(schema, { + brandedTypes: [], + generateRealEnums: true, + tuplesFromFixedArraysLengthLimit: 5, + exportNamespaces: true, + }), + ), + ).toMatchInlineSnapshot(` +"export type Main = Enums.Limit; +export namespace Enums { + export enum Limit { + _0M = "1m", + _0D = "1d", + _0W = "1w" + } +}" +`); + }); + + test('should camelize number separated identifiers', async () => { + const schema: JSONSchema7 = { + title: 'Limit', + type: 'string', + enum: ['user1name', 'user_2_name', 'user3_name'], + }; + + expect( + toSource( + await generateJSONSchemaTypes(schema, { + brandedTypes: [], + generateRealEnums: true, + tuplesFromFixedArraysLengthLimit: 5, + exportNamespaces: true, + }), + ), + ).toMatchInlineSnapshot(` +"export type Main = Enums.Limit; +export namespace Enums { + export enum Limit { + User1Name = "user1name", + User2Name = "user_2_name", + User3Name = "user3_name" + } +}" +`); + }); + test('should work with string literal enums', async () => { const schema: JSONSchema7 = { title: 'Limit', diff --git a/src/index.ts b/src/index.ts index 0e3c2cb..5f4bd4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,7 +48,12 @@ export function buildIdentifier(part: string): string { .replace(/(?:^|[^a-z0-9]+)([a-z])/gi, (_: unknown, $1: string) => $1.toUpperCase(), ) - .replace(/[^a-z0-9]/gi, ''); + .replace( + /([^a-z]+)([a-z])/gi, + (_: unknown, $1: string, $2: string) => $1 + $2.toUpperCase(), + ) + .replace(/[^a-z0-9]/gi, '') + .replace(/^[0-9]/, (_: unknown, $1: string) => '_' + $1); return identifier || 'Unknown'; }