Skip to content

Commit 05aaeaa

Browse files
committed
feat: properly dasherize class names with acronyms
1 parent 58390c6 commit 05aaeaa

2 files changed

Lines changed: 12 additions & 6 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$)/, '')
1616
.toLowerCase()
1717
if (!window.customElements.get(name)) {
1818
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

test/register.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ 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+
3137
it('automatically drops the `Element` suffix', () => {
3238
class ASuffixedElement {}
3339
register(ASuffixedElement)

0 commit comments

Comments
 (0)