Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Created January 30, 2017 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayleedev/9646c6d02314d2bb4a0a68b30eb885e9 to your computer and use it in GitHub Desktop.
Save bayleedev/9646c6d02314d2bb4a0a68b30eb885e9 to your computer and use it in GitHub Desktop.
function snakeToCamel (word, upperCamelCase) {
const sections = word.split('_')
return sections.map((section, i) => {
if (upperCamelCase || i > 0) {
return section[0].toUpperCase() + section.slice(1).toLowerCase()
}
return section.toLowerCase()
}).join('')
}
function camelToSnake (word, constantCase) {
const sections = word.split(/(?=[A-Z])/)
const method = constantCase ? 'toUpperCase' : 'toLowerCase'
return sections.map(section => section[method]()).join('_')
}
console.log(snakeToCamel('foo_bar_baz', false))
console.log(snakeToCamel('foo_bar_baz', true))
console.log(snakeToCamel('FOO_BAR_BAZ', false))
console.log(snakeToCamel('FOO_BAR_BAZ', true))
console.log(camelToSnake('fooBarBaz', false))
console.log(camelToSnake('fooBarBaz', true))
console.log(camelToSnake('FooBarBaz', false))
console.log(camelToSnake('FooBarBaz', true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment