Skip to content

Commit df0e263

Browse files
authored
Merge pull request #226 from github/dasherize-symbols
Support dasherizing symbols by using their descriptions
2 parents 25ed8e7 + 357b120 commit df0e263

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
{
5454
"path": "lib/index.js",
5555
"import": "{controller, attr, target, targets}",
56-
"limit": "1.64kb"
56+
"limit": "1.66kb"
5757
}
5858
]
5959
}

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)