Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasenmichael/28b602720d832057a51f0c622c6dc9f9 to your computer and use it in GitHub Desktop.
Save jasenmichael/28b602720d832057a51f0c622c6dc9f9 to your computer and use it in GitHub Desktop.
// camelCase
// PascalCase
// snake_case
// kebab-case
function toCamel(string) {
return string.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()).replace(/ /g, '').replace(/^\w/, c => c.toLowerCase())
}
function toPascal(string) {
return string.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()).replace(/ /g, '')
}
function toSnake(string) {
return string.toLowerCase().replace(/ /g, '_')
}
function toKekab(string) {
return string.toLowerCase().replace(/ /g, '-')
}
let string = 'yo this is a string'
console.log('camel:', toCamel(string))
console.log('Pascal:', toPascal(string))
console.log('_snake:', toSnake(string))
console.log('_SNAKE:', toSnake(string).toUpperCase())
console.log('kekab:', toKekab(string))
// outputs:
// camel: yoThisIsAString
// Pascal: YoThisIsAString_snake: yo_this_is_a_string
// _SNAKE: YO_THIS_IS_A_STRING
// kekab: yo-this-is-a-string
// https://repl.it/@jasenmichael/camel-Pascal-snake-SNAKE-kekab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment