JavaScript

Extend a Type

String.prototype.trim = function() {
        return this.replace(
         /^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}
                                                                                 
'---' + ' hello there '.trim() + '---'

String.prototype.supplant = function (o){
    return this.replace(/{([^{}]*)}/g,
                        function (a,b) {
                            return o[b];
                        }
                       );
};

var template = '<table border="{border}"' +
        '<tr><th>Last</th><td>{last}</td></tr>' +
        '<tr><th>First</th><td>{first}</td></tr>' +
        '</table>';

var data = {
        first: "Carl",
        last: "Hollywood",
        border: 2
};

template.supplant(data)

José M. Vidal .

57 of 65