Skip to content

Commit

Permalink
fix(types): avoid generating empty identifiers
Browse files Browse the repository at this point in the history
concerns #22
  • Loading branch information
nfroidure committed May 24, 2022
1 parent f174dd8 commit 2b5d554
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion fixtures/openapi/diagrams.json
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@
"title": "token_type",
"description": "See https://tools.ietf.org/html/rfc6749#section-7.1",
"type": "string",
"enum": ["bearer", "mac"]
"enum": ["bearer", "mac", "💕-✅"]
},
"expires_in": {
"description": "The lifetime in seconds of the access token",
Expand Down
21 changes: 12 additions & 9 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,8 @@ declare namespace Enums {
}
export enum TokenType {
Bearer = \\"bearer\\",
Mac = \\"mac\\"
Mac = \\"mac\\",
Unknown = \\"\\\\uD83D\\\\uDC95-\\\\u2705\\"
}
export enum Error {
InvalidRequest = \\"invalid_request\\",
Expand Down Expand Up @@ -1524,7 +1525,8 @@ declare namespace Enums {
}
export enum TokenType {
Bearer = \\"bearer\\",
Mac = \\"mac\\"
Mac = \\"mac\\",
Unknown = \\"\\\\uD83D\\\\uDC95-\\\\u2705\\"
}
export enum Role {
Admin = \\"admin\\",
Expand Down Expand Up @@ -2390,7 +2392,8 @@ declare namespace Enums {
}
export enum TokenType {
Bearer = \\"bearer\\",
Mac = \\"mac\\"
Mac = \\"mac\\",
Unknown = \\"\\\\uD83D\\\\uDC95-\\\\u2705\\"
}
export enum Error {
InvalidRequest = \\"invalid_request\\",
Expand Down Expand Up @@ -2654,7 +2657,7 @@ declare namespace Components {
export type UserArray = Components.Schemas.RequestBodiesUserArrayBody0;
export type Pet = Components.Schemas.Pet | Components.Schemas.Pet;
export type UpdatePetWithFormRequestBody = Components.Schemas.Body;
export type UploadFileRequestBody = Components.Schemas.Body_1;
export type UploadFileRequestBody = Components.Schemas.Body1;
export type PlaceOrderRequestBody = Components.Schemas.Order;
export type CreateUserRequestBody = Components.Schemas.User;
export type UpdateUserRequestBody = Components.Schemas.User;
Expand Down Expand Up @@ -2942,7 +2945,7 @@ declare namespace Components {
name?: NonNullable<string>;
status?: NonNullable<string>;
}>;
export type Body_1 = NonNullable<{
export type Body1 = NonNullable<{
additionalMetadata?: NonNullable<string>;
file?: NonNullable<string>;
}>;
Expand Down Expand Up @@ -3205,7 +3208,7 @@ declare namespace Components {
export type UserArray = Components.Schemas.RequestBodiesUserArrayBody0;
export type Pet = Components.Schemas.Pet | Components.Schemas.Pet;
export type UpdatePetWithFormRequestBody = Components.Schemas.Body;
export type UploadFileRequestBody = Components.Schemas.Body_1;
export type UploadFileRequestBody = Components.Schemas.Body1;
export type PlaceOrderRequestBody = Components.Schemas.Order;
export type CreateUserRequestBody = Components.Schemas.User;
export type UpdateUserRequestBody = Components.Schemas.User;
Expand Down Expand Up @@ -3311,7 +3314,7 @@ declare namespace Components {
name?: NonNullable<string>;
status?: NonNullable<string>;
}>;
export type Body_1 = NonNullable<{
export type Body1 = NonNullable<{
additionalMetadata?: NonNullable<string>;
file?: NonNullable<string>;
}>;
Expand Down Expand Up @@ -3622,7 +3625,7 @@ declare namespace Components {
export type UserArray = Components.Schemas.RequestBodiesUserArrayBody0;
export type Pet = Components.Schemas.Pet | Components.Schemas.Pet;
export type UpdatePetWithFormRequestBody = Components.Schemas.Body;
export type UploadFileRequestBody = Components.Schemas.Body_1;
export type UploadFileRequestBody = Components.Schemas.Body1;
export type PlaceOrderRequestBody = Components.Schemas.Order;
export type CreateUserRequestBody = Components.Schemas.User;
export type UpdateUserRequestBody = Components.Schemas.User;
Expand Down Expand Up @@ -3940,7 +3943,7 @@ declare namespace Components {
name?: NonNullable<string>;
status?: NonNullable<string>;
}>;
export type Body_1 = NonNullable<{
export type Body1 = NonNullable<{
additionalMetadata?: NonNullable<string>;
file?: NonNullable<string>;
}>;
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ export function splitRef(ref: string): string[] {
}

export function buildIdentifier(part: string): string {
return part.replace(/(?:^|[^a-z0-9]+)([a-z])/gi, (_: unknown, $1: string) =>
$1.toUpperCase(),
);
const identifier = part
.replace(/[^a-z0-9-_ ]/gi, '')
.replace(/(?:^|[^a-z0-9]+)([a-z])/gi, (_: unknown, $1: string) =>
$1.toUpperCase(),
)
.replace(/[^a-z0-9]/gi, '');

return identifier || 'Unknown';
}

async function resolve<T, U>(root: T, namespaceParts: string[]): Promise<U> {
Expand Down

0 comments on commit 2b5d554

Please sign in to comment.