forked from mdx-js/mdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to-valid-identifier-name.js
45 lines (42 loc) · 1.23 KB
/
to-valid-identifier-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {test} from 'uvu'
import * as assert from 'uvu/assert'
import {toValidIdentifierName} from '../lib/util/to-valid-identifier-name.js'
/** @param {string} invalidChar */
function toTestFailureMessage(invalidChar) {
return `Invalid characters like "${invalidChar}" should be converted to underscores "_"`
}
test('toValidIdentifierName', () => {
// Valid strings left untouched
assert.equal(toValidIdentifierName('test'), 'test')
assert.equal(toValidIdentifierName('camelCase'), 'camelCase')
// Invalid cont character -> underscore
assert.equal(
toValidIdentifierName('custom-element'),
'custom_element',
toTestFailureMessage('-')
)
assert.equal(
toValidIdentifierName('custom element'),
'custom_element',
toTestFailureMessage(' ')
)
// Invalid starting character -> underscore
assert.equal(
toValidIdentifierName('-badStarting'),
'_badStarting',
toTestFailureMessage('-')
)
assert.equal(
toValidIdentifierName('1badStarting'),
'_badStarting',
toTestFailureMessage('1')
)
assert.equal(
toValidIdentifierName(' badStarting'),
'_badStarting',
toTestFailureMessage(' ')
)
// Empty string -> underscore
assert.equal('_', toValidIdentifierName(''))
})
test.run()