Skip to content

Commit

Permalink
fix: md names handle spaces, colons, and both (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed May 10, 2024
1 parent 6acc12e commit 76e9486
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/collections/componentSetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,16 @@ export const entryToTypeAndName =
(reg: RegistryAccess) =>
(rawEntry: string): MetadataTypeAndMetadataName => {
// split on the first colon, and then join the rest back together to support names that include colons
const [typeName, ...name] = rawEntry.split(':').map((entry) => entry.trim());
const type = reg.getTypeByName(typeName);
const parent = reg.getParentType(typeName);
const [typeName, ...name] = rawEntry.split(':');
const type = reg.getTypeByName(typeName.trim());
const parent = reg.getParentType(type.name);
// If a user is requesting a child type that is unaddressable (more common with custom registries to create proper behavior)
// throw an error letting them know to use the entire parent instead
// or if they're requesting a COFT, unadressable without parent, don't throw because the parent could be requested - we don't know at this point
if (type.isAddressable === false && parent !== undefined && !type.unaddressableWithoutParent) {
throw new Error(`Cannot use this type, instead use ${parent.name}`);
}
return { type, metadataName: name.length ? name.join(':') : '*' };
return { type, metadataName: name.length ? name.join(':').trim() : '*' };
};

const typeAndNameToMetadataComponents =
Expand Down
30 changes: 30 additions & 0 deletions test/collections/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,34 @@ describe('entryToTypeAndName', () => {
metadataName: '*',
});
});
it('leading spaces in name are trimmed', () => {
expect(entryToTypeAndName(reg)('Layout: My Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('trailing spaces in name are trimmed', () => {
expect(entryToTypeAndName(reg)('Layout:My Layout ')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('spaces in name', () => {
expect(entryToTypeAndName(reg)('Layout:My Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('colons in name', () => {
expect(entryToTypeAndName(reg)('Layout:My:Colon:Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My:Colon:Layout',
});
});
it('colons and spaces in name', () => {
expect(entryToTypeAndName(reg)('Layout:My : Colon : Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My : Colon : Layout',
});
});
});

0 comments on commit 76e9486

Please sign in to comment.