url icon indicating copy to clipboard operation
url copied to clipboard

Build relative URLs with "dirty" paths

Open aminnairi opened this issue 4 years ago • 0 comments

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"

aminnairi avatar Apr 25 '21 09:04 aminnairi