Skip to content

Commit

Permalink
fix!: kebab-cases type names not correctly converted to PascalCase (#…
Browse files Browse the repository at this point in the history
…1244)

In kebab-case, the dash character is supposed to separate words, which
is currently not reflected in `normalizeTypeName`. Currently,
kebab-cased type names like "name-type" are converted to "Nametype"
instead of more expected "NameType". This patch adds a pre-processing
step which handles kebab-case first.

BREAKING CHANGE: kebab-cased type names like `name-type` were converted
to `Nametype`. Now they are converted to `NameType`.

Signed-off-by: Nikolai Prokoschenko <nikolai.prokoschenko@kurzdigital.com>
  • Loading branch information
rassie committed Nov 16, 2023
1 parent a6b353f commit 3489c31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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');
});
});

0 comments on commit 3489c31

Please sign in to comment.