diff --git a/packages/mdx/lib/util/to-valid-identifier-name.js b/packages/mdx/lib/util/to-valid-identifier-name.js new file mode 100644 index 000000000..25d70336c --- /dev/null +++ b/packages/mdx/lib/util/to-valid-identifier-name.js @@ -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 +}