Created
January 30, 2017 17:02
-
-
Save bayleedev/9646c6d02314d2bb4a0a68b30eb885e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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