Skip to content

Convert any string (even reserved words) to a valid javascript identifier.

License

Notifications You must be signed in to change notification settings

smallhelm/to-js-identifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to-js-identifier

build status dependency status

Convert any string (even reserved words) to a valid javascript identifier.

Useful when writing compile to JS languages.

var toId = require('to-js-identifier');

//valid ids just go right through
toId('foo') === 'foo'

//convert your string to a valid js identifier
toId('not-js') === 'not$45$js'

//as use can see it wraps invalid chars with '$' + c.charCodeAt(0) + '$'
//Even though $ is a valid identifier, this function will escape it, so it
//is not possible that 2 inputs generate the same output
toId('$') === '$36$'

//reserved words are escaped
toId('null') === '$null$'
toId('var')  === '$var$'

// yes I know undefined is technically an identifier, but it shouldn't be!!!
toId('undefined') === '$undefined$'

License

MIT

About

Convert any string (even reserved words) to a valid javascript identifier.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published