Skip to content

Commit 7ef79d4

Browse files
committed
Support dasherizing symbols by using their descriptions
1 parent 812f059 commit 7ef79d4

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/dasherize.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
export const dasherize = (str: unknown): string =>
2-
String(str)
2+
String(typeof str === 'symbol' ? str.description : str)
33
.replace(/([A-Z]($|[a-z]))/g, '-$1')
44
.replace(/--/g, '-')
55
.replace(/^-|-$/, '')
66
.toLowerCase()
7+
8+
export const mustDasherize = (str: unknown, type = 'property'): string => {
9+
const dashed = dasherize(str)
10+
if (!dashed.includes('-')) {
11+
throw new DOMException(`${type}: ${String(str)} is not a valid ${type} name`, 'SyntaxError')
12+
}
13+
return dashed
14+
}

test/dasherize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ describe('dasherize', () => {
88
['FooBar', 'foo-bar'],
99
['autofocusWhenReady', 'autofocus-when-ready'],
1010
['URLBar', 'url-bar'],
11-
['ClipX', 'clip-x']
11+
['ClipX', 'clip-x'],
12+
[Symbol('helloWorld'), 'hello-world']
1213
]
1314

1415
tests.map(([input, output]) =>
15-
it(`transforms ${input} to ${output}`, () => expect(dasherize(input)).to.equal(output))
16+
it(`transforms ${String(input)} to ${output}`, () => expect(dasherize(input)).to.equal(output))
1617
)
1718
})

0 commit comments

Comments
 (0)