Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: kebab-cases type names not correctly converted to PascalCase #1244

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/type-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ export class TypeGenerator {
* (e.g. "Vpc", "FooBarZooFiGoo").
*/
public static normalizeTypeName(typeName: string) {

// Handle kebab-case first
const stage1 = typeName.split('-').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join('');

// start with the full string and then use the regex to match all-caps sequences.
const re = /([A-Z]+)(?:[^a-z]|$)/g;
let result = typeName;
let result = stage1;
let m;
do {
m = re.exec(typeName);
m = re.exec(stage1);
if (m) {
const before = result.slice(0, m.index); // all the text before the sequence
const cap = m[1]; // group #1 matches the all-caps sequence we are after
Expand Down
10 changes: 9 additions & 1 deletion test/type-generator.naming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ describe('normalizeTypeName', () => {
expect(TypeGenerator.normalizeTypeName('attributemanifestsOptions')).toEqual('AttributemanifestsOptions');
expect(TypeGenerator.normalizeTypeName('attributeMANIFESTSOptions')).toEqual('AttributeManifestsOptions');
});
});
test('Kebab-case names are converted to PascalCase', () => {
expect(TypeGenerator.normalizeTypeName('AttributeManifests')).toEqual('AttributeManifests');
expect(TypeGenerator.normalizeTypeName('Attribute-Manifests')).toEqual('AttributeManifests');
expect(TypeGenerator.normalizeTypeName('Attribute-manifests')).toEqual('AttributeManifests');
expect(TypeGenerator.normalizeTypeName('attribute-manifests')).toEqual('AttributeManifests');
expect(TypeGenerator.normalizeTypeName('attribute-manifestsOptions')).toEqual('AttributeManifestsOptions');
expect(TypeGenerator.normalizeTypeName('attribute-MANIFESTSOptions')).toEqual('AttributeManifestsOptions');
});
});