Last active
February 1, 2016 09:31
-
-
Save sunpietro/ad0100f6e5665dc70ac5 to your computer and use it in GitHub Desktop.
Formats a string like sprintf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var formatString = (function() | |
{ | |
var replacer = function(context) | |
{ | |
return function(s, name) | |
{ | |
return context[name]; | |
}; | |
}; | |
return function(input, context) | |
{ | |
return input.replace(/\{(\w+)\}/g, replacer(context)); | |
}; | |
})(); | |
//usage | |
formatString("Hello {name}, {greeting}", {name: "Steve", greeting: "how's it going?"}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment