odin-default-js-exercises/snakeCase/snakeCase.js

21 lines
439 B
JavaScript

const snakeCase = function(string) {
// wtf case
string= string.replace(/[(\.\.)-]/g, " ");
// this splits up camelcase IF there are no spaces in the word
if (string.indexOf(" ") < 0) {
string = string.replace(/([A-Z])/g, " $1");
}
return string
.trim()
.toLowerCase()
.replace(/[,\?\.]/g, "")
.replace(/\-/g, " ")
.split(" ")
.join("_")
.replace(/(__)/g,'_');
};
module.exports = snakeCase;