Skip to content

Instantly share code, notes, and snippets.

@JonathanDn
Last active March 24, 2017 11:07
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 JonathanDn/641e3eba4f30afc3fd82f0fe62d3a462 to your computer and use it in GitHub Desktop.
Save JonathanDn/641e3eba4f30afc3fd82f0fe62d3a462 to your computer and use it in GitHub Desktop.
URL constructing function with ascending arguments
export function format(str, argumentsArr?) {
let urlConstruct = str;
for (var i = 0; i < argumentsArr.length; i++) {
// g - global search, m - multiline
let regEx = new RegExp("\\{" + (i) + "\\}", 'gm');
// Handle multiple whitespaces in STRING type arguments
if (typeof(argumentsArr[i]) === 'string') {
argumentsArr[i] = argumentsArr[i].replace(/\s/g, '');
}
// Add to url construct
urlConstruct = urlConstruct.replace(regEx, argumentsArr[i]);
}
return urlConstruct;
}
// Will work like so(endless amount of args):
// url should look like this to work : http://{0}/{1}/{2} and so on...
format(str, 1stArg?, 2ndArg?, 3rdArg?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment