Skip to content

Instantly share code, notes, and snippets.

@fatihturgut
Last active July 9, 2019 16:50
Show Gist options
  • Save fatihturgut/2ab33ed629b2683adb02d2ff90216c74 to your computer and use it in GitHub Desktop.
Save fatihturgut/2ab33ed629b2683adb02d2ff90216c74 to your computer and use it in GitHub Desktop.
Parameterize String
/***
* @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