Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build relative URLs with "dirty" paths #48

Open
aminnairi opened this issue Apr 25, 2021 · 0 comments
Open

Build relative URLs with "dirty" paths #48

aminnairi opened this issue Apr 25, 2021 · 0 comments

Comments

@aminnairi
Copy link

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant