Re: [whatwg/url] Support relative URLs (#531)

I hope @alwinb doesn’t mind me advertising [their library][reurl] here (nor anyone else, for that matter), but I recently found it through <https://github.com/whatwg/url/issues/405#issuecomment-694786491>, and it allow manipulating relative URLs and resolving them against other (relative or absolute) URLs in a way that complies to this specification.

[reurl]: https://github.com/alwinb/reurl


It’s really simple, actually!

~~~ JavaScript
let url = new Url("../messages/goodbye.txt")
url = url.set({file: "hello.txt"})
console.log(url.host, [...url.dirs], url.file) // null, ["..", "messages"], "hello.txt"

console.log(new Url("https://example.com/things/index.html").goto(url).force().normalize().href) // "https://example.com/messages/hello.txt"
~~~

A couple notes:

+ `.normalize()` will collapse `.` and `..` appropriately.
+ `.force()` will ensure special URLs have a host. (In this example, it’s unnecessary).
+ URL objects appear to be immutable. (From what I was able to check.)
+ When parsing a relative URL, you can specify the parsing mode (“special” vs. “`file`” vs. “regular”) with an argument to the `Url` constructor. (It defaults to non‐`file` special, i.e. similar to `http[s]` and `ws[s]`.)
+ You can construct URL object from “parts” instead of from a string. (Relevant to #354.)
+ By default, `.toString()` will produce a string that can contain non‐ASCII characters. `.toASCII()` (or equivalently, `.toJSON()` or `.href`) will produce an ASCII string, using percent‐encodings and punycode as appropriate.

Maybe this library can serve as inspiration of some kind for an API for the spec.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/531#issuecomment-696422606

Received on Monday, 21 September 2020 22:58:14 UTC