Skip to content

Instantly share code, notes, and snippets.

@Harshakvarma
Last active August 31, 2020 18:33
Show Gist options
  • Save Harshakvarma/f595aeb57a34401d60a110b0f4b59108 to your computer and use it in GitHub Desktop.
Save Harshakvarma/f595aeb57a34401d60a110b0f4b59108 to your computer and use it in GitHub Desktop.
Javascript/Python converting case
/* https://medium.com/better-programming/string-case-styles-camel-pascal-snake-and-kebab-case-981407998841 */
const stringConvert = s => s.split(' ').join('-')
console.log("String to Kebab Case:",stringConvert('this is awesome'))
// Ex: convertCamelCase("this-is-awesome")
// Ans: thisIsAwesome
const convertCamelCase = s => s.replace(/-./g, x=>x.toUpperCase()[1])
console.log("Camel Case:",convertCamelCase("this-is-awesome"))
// Ex: convertPascalCase("this-is-Awesome")
// Ans: ThisIsAwesome
const convertPascalCase = s => s.replace(/(^\w|-\w)/g, x => x.replace(/-/, "").toUpperCase())
console.log("Pascal Case:",convertPascalCase("this-is-Awesome"))
// Ex: convertSnakeCase("thisIsAwesome")
// Ans: this_is_awesome
const convertSnakeCase = s => s.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1_$2').toLowerCase()
console.log("Snake Case:",convertSnakeCase("thisIsAwesome"))
// Ex: covertKebabCase("thisIsAwesome")
// Ans: this-is-awesome
const covertKebabCase = s => s.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
console.log("Kebab Case: ",covertKebabCase("thisIsAwesome"))
""" https://medium.com/better-programming/string-case-styles-camel-pascal-snake-and-kebab-case-981407998841 """
// TRYING to add to string proptype
String.prototype.convertCamelCase = function() {this.replace(/-./g, x=>x.toUpperCase()[1])}
console.log("Camel Case:","this-is-awesome".convertCamelCase())
// Ex: convertPascalCase("this-is-Awesome")
// Ans: ThisIsAwesome
String.prototype.convertPascalCase = function() {this.replace(/(^\w|-\w)/g, x => x.replace(/-/, "").toUpperCase())}
console.log("Pascal Case:","this-is-Awesome".convertPascalCase())
// Ex: convertSnakeCase("thisIsAwesome")
// Ans: this_is_awesome
String.prototype.convertSnakeCase = function() {this.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1_$2').toLowerCase()}
console.log("Snake Case:","thisIsAwesome".convertSnakeCase())
// Ex: covertKebabCase("thisIsAwesome")
// Ans: this-is-awesome
String.prototype.covertKebabCase = function() {this.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()}
console.log("Kebab Case:","thisIsAwesome".covertKebabCase())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment