Last active
November 14, 2019 14:16
-
-
Save andrijac/ef4ac4340884d319b41c9b7a276bf037 to your computer and use it in GitHub Desktop.
This file contains 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
function stringFormat(format, args) { | |
if (arguments.length === 0) { | |
throw "Missing arguments"; | |
} | |
if (!args) { | |
return format; | |
} | |
return format.replace(/{(\d+)}/g, function(match, number) { | |
return typeof args[number] !== 'undefined' ? args[number] : match; | |
}); | |
} | |
stringFormat("{0} {1} {2}", ["1", "22"]) | |
// "1 22 {2}" | |
stringFormat("asd") | |
// "asd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment