Skip to content

Commit 11b96d3

Browse files
committed
feat: add toValidIdentifierName util
1 parent 529b96a commit 11b96d3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {start, cont} from 'estree-util-is-identifier-name'
2+
3+
/**
4+
* Replace all invalid identifier characters with underscores "_"
5+
* @param {string} str
6+
*/
7+
export function toValidIdentifierName(string_) {
8+
if (string_.length === 0) return '_'
9+
let validString = ''
10+
validString += start(string_[0].charCodeAt(0)) ? string_[0] : '_'
11+
for (const char of string_.slice(1)) {
12+
validString += cont(char.charCodeAt(0)) ? char : '_'
13+
}
14+
15+
return validString
16+
}

0 commit comments

Comments
 (0)