17 Jul Function in Coffescript and Javascript to convert a Camel Case class into Snake Case (underscore)
In this case I want to show the method wich a created because I need to convert in camel case format “DebitCard” into snake case (underscore) format “debit_card” and here you can find what I did:
Coffeescript
snakeCase: (clazz)-> unless clazz is '' or clazz is undefined clazz.replace /([A-Z])/g, ($1) -> if clazz.charAt(0) is $1 $1.toLowerCase() else "_" + $1.toLowerCase()
JAVASCRIPT
my_object = ({ snakeCase: function(clazz) { if (!(clazz === '' || clazz === void 0)) { return clazz.replace(/([A-Z])/g, function($1) { if (clazz.charAt(0) === $1) { return $1.toLowerCase(); } else { return "_" + $1.toLowerCase(); } }); } } }); my_object.snakeCase("DebitCard") => "debit_card"
No Comments