wonderbeyond
12/20/2016 - 7:18 AM

Most simple string format utility

Most simple string format utility

function format(str, ctx) {
    /**
     * Examples:
     *  format('hello, {name}', {name: 'wonder'})
     *  format('{0}, {1}', ['hello', 'world'])
     */
    return str.replace(/\{(\w+)\}/g, function(m, p1) {
        return ctx[p1];
    });
}