Skip to content

Build relative URLs with "dirty" paths #48

@aminnairi

Description

@aminnairi

Context

What I mean by a "dirty" path is simply a path that has trailing and leading slashes, without accounting for what other URL parts are following or leading.

Current behavior

If we try to merge parts of a URL that contains leading and trailing slashes, building a relative path out of these data would lead to an incorrect path.

import Url.Builder

Url.Builder.relative ["http://localhost/", "/ping/"] [] -- "http://localhost///ping/"

Expected behavior

It would be nice if the function somehow cleaned the URLs. The end result (with the above data) could look like that.

import Url.Builder

Url.Builder.relative ["http://localhost/", "/ping/"]  [] -- "http://localhost/ping/"
Url.Builder.relative ["http://localhost/", "ping/"]   [] -- "http://localhost/ping/"
Url.Builder.relative ["http://localhost/", "/ping"]   [] -- "http://localhost/ping"
Url.Builder.relative ["http://localhost/", "ping"]    [] -- "http://localhost/ping"
Url.Builder.relative ["http://localhost", "/ping/"]   [] -- "http://localhost/ping/"
Url.Builder.relative ["http://localhost", "ping/"]    [] -- "http://localhost/ping/"
Url.Builder.relative ["http://localhost", "/ping"]    [] -- "http://localhost/ping"
Url.Builder.relative ["http://localhost", "ping"]     [] -- "http://localhost/ping"

Additional Context

In JavaScript, there is a module path that has a join function that act in a similar fashion, but will merge the parts of a path and make a valid url at the end (it also uses the operating system's default path separator but this is irrelevant here).

There could be a new Url.Builder.join function to prevent any breaking change in the current API instead.

import {join} from "path";

console.log(join("http://localhost/", "/ping/")); // "http://localhost/ping/"
console.log(join("http://localhost/", "ping/"));  // "http://localhost/ping/"
console.log(join("http://localhost/", "/ping"));  // "http://localhost/ping"
console.log(join("http://localhost/", "ping"));   // "http://localhost/ping"
console.log(join("http://localhost", "/ping/"));  // "http://localhost/ping/"
console.log(join("http://localhost", "ping/"));   // "http://localhost/ping/"
console.log(join("http://localhost", "/ping"));   // "http://localhost/ping"
console.log(join("http://localhost", "ping"));    // "http://localhost/ping"

Like

import Url.Builder

Url.Builder.join ["http://localhost/", "/ping/"]  [] -- "http://localhost/ping/"
Url.Builder.join ["http://localhost/", "ping/"]   [] -- "http://localhost/ping/"
Url.Builder.join ["http://localhost/", "/ping"]   [] -- "http://localhost/ping"
Url.Builder.join ["http://localhost/", "ping"]    [] -- "http://localhost/ping"
Url.Builder.join ["http://localhost", "/ping/"]   [] -- "http://localhost/ping/"
Url.Builder.join ["http://localhost", "ping/"]    [] -- "http://localhost/ping/"
Url.Builder.join ["http://localhost", "/ping"]    [] -- "http://localhost/ping"
Url.Builder.join ["http://localhost", "ping"]     [] -- "http://localhost/ping"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions