Skip to content

Instantly share code, notes, and snippets.

@exsesx
Last active April 27, 2022 13:30
Show Gist options
  • Select an option

  • Save exsesx/90b4a77ce80a3d1218dffdbab1f7c675 to your computer and use it in GitHub Desktop.

Select an option

Save exsesx/90b4a77ce80a3d1218dffdbab1f7c675 to your computer and use it in GitHub Desktop.

Revisions

  1. exsesx revised this gist Apr 27, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion nodejs-join-urls.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@ Tested with Node.js LTS (16 at the time of the gist creation).
    buildUrl('http://example.com', 'a', 'b', 'c').toString() // http://example.com/a/b/c
    ```

    > Note that the function returns a [URL](https://nodejs.org/api/url.html) object, not a string.
    > Note that the function returns a [URL](https://nodejs.org/api/url.html) object, not a string.
    > This gist is mostly inspired by a post on [StackOverflow](https://stackoverflow.com/questions/16301503/can-i-use-requirepath-join-to-safely-concatenate-urls).
  2. exsesx revised this gist Apr 27, 2022. 1 changed file with 3 additions and 20 deletions.
    23 changes: 3 additions & 20 deletions nodejs-join-urls.md
    Original file line number Diff line number Diff line change
    @@ -5,24 +5,7 @@ An easy solution to a common problem.
    Tested with Node.js LTS (16 at the time of the gist creation).

    ```javascript
    const result = buildUrl('http://example.com', 'a', 'b', 'c')
    buildUrl('http://example.com', 'a', 'b', 'c').toString() // http://example.com/a/b/c
    ```

    /*
    URL {
    href: 'http://example.com/a/b/c',
    origin: 'http://example.com',
    protocol: 'http:',
    username: '',
    password: '',
    host: 'example.com',
    hostname: 'example.com',
    port: '',
    pathname: '/a/b/c',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
    }
    */

    result.toString() // http://example.com/a/b/c
    ```
    > Note that the function returns a [URL](https://nodejs.org/api/url.html) object, not a string.
  3. exsesx revised this gist Apr 27, 2022. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions nodejs-join-urls.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    # Usage

    ```javascript
    const result = buildUrl('http://example.com', 'a', 'b', 'c')

    result.toString() // http://example.com/a/b/c
    An easy solution to a common problem.

    Tested with Node.js LTS (16 at the time of the gist creation).

    ```javascript
    const result = buildUrl('http://example.com', 'a', 'b', 'c')

    /*
    URL {
    @@ -23,4 +23,6 @@ URL {
    hash: ''
    }
    */

    result.toString() // http://example.com/a/b/c
    ```
  4. exsesx revised this gist Apr 27, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions nodejs-join-urls.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@ const result = buildUrl('http://example.com', 'a', 'b', 'c')

    result.toString() // http://example.com/a/b/c



    /*
    URL {
    href: 'http://example.com/a/b/c',
  5. exsesx revised this gist Apr 27, 2022. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions nodejs-join-urls.md
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,9 @@
    ```javascript
    const result = buildUrl('http://example.com', 'a', 'b', 'c')

    /*
    result.toString() // http://example.com/a/b/c

    /*
    URL {
    href: 'http://example.com/a/b/c',
    origin: 'http://example.com',
    @@ -19,8 +20,5 @@ URL {
    searchParams: URLSearchParams {},
    hash: ''
    }
    */

    result.toString() // http://example.com/a/b/c
    ```
  6. exsesx renamed this gist Apr 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. exsesx renamed this gist Apr 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. exsesx renamed this gist Apr 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. exsesx created this gist Apr 27, 2022.
    26 changes: 26 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # Usage

    ```javascript
    const result = buildUrl('http://example.com', 'a', 'b', 'c')

    /*
    URL {
    href: 'http://example.com/a/b/c',
    origin: 'http://example.com',
    protocol: 'http:',
    username: '',
    password: '',
    host: 'example.com',
    hostname: 'example.com',
    port: '',
    pathname: '/a/b/c',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
    }
    */

    result.toString() // http://example.com/a/b/c
    ```
    9 changes: 9 additions & 0 deletions urls.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const path = require('path');

    function buildUrl(base, ...inputs) {
    return new URL(path.posix.join(...inputs), base);
    }

    module.exports = {
    buildUrl,
    };
    5 changes: 5 additions & 0 deletions urls.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import path from 'path';

    export function buildUrl(base, ...inputs) {
    return new URL(path.posix.join(...inputs), base);
    }
    5 changes: 5 additions & 0 deletions urls.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import path from 'path';

    export function buildUrl(base: string, ...inputs: string[]): URL {
    return new URL(path.posix.join(...inputs), base);
    }