Skip to content

Commit

Permalink
feat: add toValidIdentifierName util
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 11, 2022
1 parent 529b96a commit 11b96d3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/mdx/lib/util/to-valid-identifier-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {start, cont} from 'estree-util-is-identifier-name'

/**
* Replace all invalid identifier characters with underscores "_"
* @param {string} str
*/
export function toValidIdentifierName(string_) {
if (string_.length === 0) return '_'
let validString = ''
validString += start(string_[0].charCodeAt(0)) ? string_[0] : '_'
for (const char of string_.slice(1)) {
validString += cont(char.charCodeAt(0)) ? char : '_'
}

return validString
}

0 comments on commit 11b96d3

Please sign in to comment.