You want to have a look at LoDash
or Underscore
or Slugify
for more reliable version. This is just for fun, and probably might not suitable for any production use 🤓
So, I wanted to kebabCase
a string
literal without having to refer any of the aboves. After reading few js libraries mentioned above, this is what I ended up.
function kebabCase(value, separator = '_') {
// Expression to trim trailings
var defaultToWS = '[' + separator.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1') + ']';