MagicCasing
by lasha
JavaScript
function magicCasing(str, custom){
var defaultLib = {
"rsvp": "RSVP",
"Rsvp": "RSVP",
"html": "HTML",
"Html": "HTML"
}
// Extend the defaultLib with the key/vaks from 'custom' parameter
if(typeof custom !== "undefined"){
for(var prop in custom) {
//if(defaultLib.hasOwnProperty(prop)) {
defaultLib[prop] = custom[prop];
//}
}
}
var strArray = str.match(/\w+/g);
var newStr = str;
strArray.forEach(function(el, i){
var word = el
if( word !== null && defaultLib.hasOwnProperty(word) ){
newStr = newStr.replace(word, defaultLib[word]);
}
});
return newStr;
}
console.log( magicCasing("Please submit youre rsvp! Oh, and don't forget the html.") );
console.log( magicCasing("Please submit youre rsvp! Oh, and don't forget the html.", {"youre": "you're", "html":"hTmL"}) );
console.log( magicCasing("Please -submit- youre rsvp! Oh, and don't forget the html.", {"submit": "SUBMIT"}) );