Skip to content

Instantly share code, notes, and snippets.

@huanglong-zz
Forked from maggiben/minimustache.functional.js
Created May 14, 2014 07:51
Show Gist options
  • Save huanglong-zz/47fc0ca1fe648563a810 to your computer and use it in GitHub Desktop.
Save huanglong-zz/47fc0ca1fe648563a810 to your computer and use it in GitHub Desktop.
String.prototype.format = function() {
var formatted = this;
var arg = arguments[0];
var regexp = null;
var key = null;
if(arg === Object(arg) && arg instanceof Array !== true) {
for(key in arg) {
if(arg.hasOwnProperty(key)) {
regexp = new RegExp('\\{{' + key + '\\}}', 'gi');
formatted = formatted.replace(regexp, arg[key]);
}
}
} else {
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{{' + i + '\\}}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
}
return formatted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment