JS Test - optimization, functionality imrpovement

by lasha

HTML

Original:<br><span class="original">Please -submit- your rsvp! Oh, and don't forget the html.</span><br><br>
Modified: <span class="modified"></span>

JavaScript

// Task: Prevent special characters (. ! ' , -) from being stripped out.

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);
    
    strArray.forEach(function(el, i){
        var word = el
        
        if( word !== null && defaultLib.hasOwnProperty(word) ){
            strArray[i] = strArray[i].replace(word, defaultLib[word]);
        }
    });
    
    return strArray.join(" ");
}

document.querySelector('.modified').innerHTML = magicCasing(document.querySelector('.original').innerHTML);