Skip to content

Commit 622b552

Browse files
committed
Change to support code points, support astrals
1 parent a40a7b2 commit 622b552

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

lib/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ const contRe = /[$_\u{200C}\u{200D}\p{ID_Continue}]/u
33
const re = /^[$_\p{ID_Start}][$_\u{200C}\u{200D}\p{ID_Continue}]*$/u
44

55
/**
6-
* Checks if the given character code can start an identifier.
6+
* Checks if the given code point can start an identifier.
77
*
8-
* @param {number} code
9-
* Character code to check.
8+
* @param {number | undefined} code
9+
* Code point to check.
1010
* @returns {boolean}
1111
* Whether `code` can start an identifier.
1212
*/
13-
// To do: support astrals.
13+
// Note: `undefined` is supported so you can pass the result from `''.codePointAt`.
1414
export function start(code) {
15-
return startRe.test(String.fromCharCode(code))
15+
return code ? startRe.test(String.fromCodePoint(code)) : false
1616
}
1717

1818
/**
19-
* Checks if the given character code can continue an identifier.
19+
* Checks if the given code point can continue an identifier.
2020
*
21-
* @param {number} code
22-
* Character code to check.
21+
* @param {number | undefined} code
22+
* Code point to check.
2323
* @returns {boolean}
2424
* Whether `code` can continue an identifier.
2525
*/
26-
// To do: support astrals.
26+
// Note: `undefined` is supported so you can pass the result from `''.codePointAt`.
2727
export function cont(code) {
28-
return contRe.test(String.fromCharCode(code))
28+
return code ? contRe.test(String.fromCodePoint(code)) : false
2929
}
3030

3131
/**

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@
5858
"trailingComma": "none"
5959
},
6060
"xo": {
61-
"prettier": true,
62-
"rules": {
63-
"no-misleading-character-class": "off",
64-
"unicorn/prefer-code-point": "off"
65-
}
61+
"prettier": true
6662
},
6763
"remarkConfig": {
6864
"plugins": [

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ name('$something69') // => true
6868
name('69') // => false
6969
name('var') // => true (this does not handle keywords)
7070

71-
start(48) // => false (character code for `0`)
72-
cont(48) // => true (character code for `0`)
71+
start(48) // => false (code point for `'0'`)
72+
cont(48) // => true (code point for `'0'`)
7373
```
7474

7575
## API
@@ -80,12 +80,12 @@ There is no default export.
8080

8181
### `cont(code)`
8282

83-
Checks if the given character code can continue an identifier.
83+
Checks if the given code point can continue an identifier.
8484

8585
###### Parameters
8686

8787
* `code` (`number`)
88-
character code to check
88+
— code point to check
8989

9090
###### Returns
9191

@@ -106,12 +106,12 @@ Whether `name` can be an identifier (`boolean`).
106106

107107
### `start(code)`
108108

109-
Checks if the given character code can start an identifier.
109+
Checks if the given code point can start an identifier.
110110

111111
###### Parameters
112112

113113
* `code` (`number`)
114-
character code to check
114+
— code point to check
115115

116116
###### Returns
117117

test.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,35 @@ test('isIdentifierName', () => {
1010
'should expose the public api'
1111
)
1212

13-
assert.ok(start('a'.charCodeAt(0)), 'should say `a` is a start code')
14-
assert.ok(start('_'.charCodeAt(0)), 'should say `_` is a start code')
15-
assert.ok(!start('1'.charCodeAt(0)), 'should not say `1` is a start code')
16-
assert.ok(!start('-'.charCodeAt(0)), 'should not say `-` is a start code')
13+
assert.ok(
14+
!start(''.codePointAt(0)),
15+
'should not say `undefined` is a start code'
16+
)
17+
assert.ok(start('a'.codePointAt(0)), 'should say `a` is a start code')
18+
assert.ok(start('_'.codePointAt(0)), 'should say `_` is a start code')
19+
assert.ok(!start('1'.codePointAt(0)), 'should not say `1` is a start code')
20+
assert.ok(!start('-'.codePointAt(0)), 'should not say `-` is a start code')
21+
assert.ok(start('ಠ'.codePointAt(0)), 'should say `ಠ` is a start code')
22+
assert.ok(
23+
start(0x1_02_a7),
24+
'should say `0xd8_00 0xde_a7` (together 0x1_02_a7) is a start code'
25+
)
26+
assert.ok(!start(0xd8_00), 'should not say `0xd8_00` is a start code')
1727

18-
assert.ok(cont('a'.charCodeAt(0)), 'should say `a` is a cont code')
19-
assert.ok(cont('_'.charCodeAt(0)), 'should say `_` is a cont code')
20-
assert.ok(cont('1'.charCodeAt(0)), 'should say `1` is a cont code')
21-
assert.ok(!cont('-'.charCodeAt(0)), 'should not say `-` is a cont code')
28+
assert.ok(
29+
!cont(''.codePointAt(0)),
30+
'should not say `undefined` is a cont code'
31+
)
32+
assert.ok(cont('a'.codePointAt(0)), 'should say `a` is a cont code')
33+
assert.ok(cont('_'.codePointAt(0)), 'should say `_` is a cont code')
34+
assert.ok(cont('1'.codePointAt(0)), 'should say `1` is a cont code')
35+
assert.ok(!cont('-'.codePointAt(0)), 'should not say `-` is a cont code')
36+
assert.ok(cont('ಠ'.codePointAt(0)), 'should say `ಠ` is a cont code')
37+
assert.ok(
38+
cont(0x1_02_a7),
39+
'should say `0xd8_00 0xde_a7` (together 0x1_02_a7) is a cont code'
40+
)
41+
assert.ok(!cont(0xd8_00), 'should not say `0xd8_00` is a cont code')
2242

2343
assert.ok(!name(''), 'should not say `` (empty string) is a name')
2444
assert.ok(name('a'), 'should say `a` is a name')
@@ -30,4 +50,6 @@ test('isIdentifierName', () => {
3050
assert.ok(name('_a$b9'), 'should say `_a$b9` is a name')
3151
assert.ok(!name('aaa-'), 'should not say `aaa-` is a name')
3252
assert.ok(!name('-aaa'), 'should not say `-aaa` is a name')
53+
assert.ok(name('ಠ_ಠ'), 'should say `ಠ_ಠ` is a name')
54+
assert.ok(name('𐊧'), 'should say `𐊧` (0x1_02_a7) is a name')
3355
})

0 commit comments

Comments
 (0)