Files
odin-javascript-exercises/snakeCase/snakeCase.js
T

20 lines
409 B
JavaScript
Raw Normal View History

2017-12-15 13:26:05 -06:00
const snakeCase = function(string) {
// wtf case
string = string.replace(/\.\./g, " ");
2017-08-25 14:16:42 -05:00
2017-12-15 13:26:05 -06:00
// this splits up camelcase IF there are no spaces in the word
if (string.indexOf(" ") < 0) {
string = string.replace(/([A-Z])/g, " $1");
}
2017-08-25 14:16:42 -05:00
2017-12-15 13:26:05 -06:00
return string
.trim()
.toLowerCase()
.replace(/[,\?\.]/g, "")
.replace(/\-/g, " ")
.split(" ")
.join("_");
};
module.exports = snakeCase;