Skip to content

Commit ee3f1c4

Browse files
authored
Merge pull request #44 from github/feat-properly-dasherize-class-names-with-acronyms
feat: properly dasherize class names with acronyms
2 parents 41cdbda + 606c825 commit ee3f1c4

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

src/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ interface CustomElement {
1111
*/
1212
export function register(classObject: CustomElement): void {
1313
const name = classObject.name
14-
.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-')
15-
.replace(/-Element$/, '')
14+
.replace(/([A-Z]($|[a-z]))/g, '-$1')
15+
.replace(/(^-|-Element$)/g, '')
1616
.toLowerCase()
1717
if (!window.customElements.get(name)) {
1818
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

test/register.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ describe('register', () => {
2121
})
2222

2323
it('dasherises class names', () => {
24-
class ThisIsAnExampleOfADasherisedClassName {}
25-
register(ThisIsAnExampleOfADasherisedClassName)
26-
expect(window.customElements.get('this-is-an-example-of-a-dasherised-class-name')).to.equal(
27-
ThisIsAnExampleOfADasherisedClassName
24+
class ThisIsAnExampleOfDasherisedClassNames {}
25+
register(ThisIsAnExampleOfDasherisedClassNames)
26+
expect(window.customElements.get('this-is-an-example-of-dasherised-class-names')).to.equal(
27+
ThisIsAnExampleOfDasherisedClassNames
2828
)
2929
})
3030

31+
it('will intuitively dasherize acryonyms', () => {
32+
class URLBar {}
33+
register(URLBar)
34+
expect(window.customElements.get('url-bar')).to.equal(URLBar)
35+
})
36+
37+
it('dasherizes cap suffixed names correctly', () => {
38+
class ClipX {}
39+
register(ClipX)
40+
expect(window.customElements.get('clip-x')).to.equal(ClipX)
41+
})
42+
3143
it('automatically drops the `Element` suffix', () => {
32-
class ASuffixedElement {}
33-
register(ASuffixedElement)
34-
expect(window.customElements.get('a-suffixed')).to.equal(ASuffixedElement)
44+
class AutoCompleteElement {}
45+
register(AutoCompleteElement)
46+
expect(window.customElements.get('auto-complete')).to.equal(AutoCompleteElement)
3547
})
3648
})

0 commit comments

Comments
 (0)