Skip to content

Instantly share code, notes, and snippets.

@GitHub30
Created March 19, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitHub30/a398ea72c62d52f9ec91 to your computer and use it in GitHub Desktop.
Save GitHub30/a398ea72c62d52f9ec91 to your computer and use it in GitHub Desktop.
String.prototype.format = String.prototype.f = function(arg){
var is_object = typeof arg === 'object',
keys = is_object ? Object.keys(arg) : (function(l){for(i=0,a=[];i<l;i++)a[i]=i;return a;})(arguments.length),
args = is_object ? arg : arguments;
return this.replace( new RegExp('\\{(keys)\\}'.replace('keys', keys.join('|')), 'gm'), function(match, key){return args[key];});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment