Skip to content

Instantly share code, notes, and snippets.

@JonathanDn
Last active March 24, 2017 11:07

Revisions

  1. JonathanDn revised this gist Mar 24, 2017. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions url-constructor.ts
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,18 @@
    export function urlFormat(str) {
    export function format(str, argumentsArr?) {
    let urlConstruct = str;
    for (var i = 1; i < arguments.length; i++) {
    for (var i = 0; i < argumentsArr.length; i++) {
    // g - global search, m - multiline
    let regEx = new RegExp("\\{" + (i - 1) + '"\\}', 'gm')
    urlConstruct = urlConstruct.replace(regEx, arguments[i]);
    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...
    urlFormat(str, 1stArg?, 2ndArg?, 3rdArg?)
    format(str, 1stArg?, 2ndArg?, 3rdArg?)
  2. JonathanDn revised this gist Mar 24, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions url-constructor.ts
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,5 @@ export function urlFormat(str) {
    }

    // Will work like so(endless amount of args):
    // url should look like this to work : http://{0}/{1}/{2} and so on...
    urlFormat(str, 1stArg?, 2ndArg?, 3rdArg?)
  3. JonathanDn renamed this gist Mar 24, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. JonathanDn created this gist Mar 24, 2017.
    12 changes: 12 additions & 0 deletions url.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    export function urlFormat(str) {
    let urlConstruct = str;
    for (var i = 1; i < arguments.length; i++) {
    // g - global search, m - multiline
    let regEx = new RegExp("\\{" + (i - 1) + '"\\}', 'gm')
    urlConstruct = urlConstruct.replace(regEx, arguments[i]);
    }
    return urlConstruct;
    }

    // Will work like so(endless amount of args):
    urlFormat(str, 1stArg?, 2ndArg?, 3rdArg?)