Last active
July 9, 2019 16:50
-
-
Save fatihturgut/2ab33ed629b2683adb02d2ff90216c74 to your computer and use it in GitHub Desktop.
Parameterize String
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
/*** | |
* @example parameterizedString("my name is %s1 and surname is %s2", "John", "Doe"); | |
* @return "my name is John and surname is Doe" | |
* | |
* @firstArgument {String} like "my name is %s1 and surname is %s2" | |
* @otherArguments {String | Number} | |
* @returns {String} | |
*/ | |
export const parameterizedString = (...args) => { | |
const str = args[0]; | |
const params = args.filter((arg, index) => index !== 0); | |
if (!str) return ""; | |
return str.replace(/%s[0-9]+/g, matchedStr => { | |
const variableIndex = matchedStr.replace("%s", "") - 1; | |
return params[variableIndex]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment