- From: Jimmy Wärting <notifications@github.com>
- Date: Thu, 05 Jan 2023 08:55:25 -0800
- To: whatwg/url <url@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Thursday, 5 January 2023 16:55:38 UTC
my example of coming up with something like a relative URL constructor (b/c NodeJS dose not have an origin or location like deno)
```js
const URLfrom = ((_URL => (origin = 'file://') => {
return /** @type {typeof URL} */ (Object.assign(function URL(url, base) {
return new _URL(url, new _URL(base, origin))
}, _URL))
})(globalThis.URL))
// Example usage:
const RelativeURL = URLfrom('https://httpbin.org')
new RelativeURL('/get').toString() // https://httpbin.org/get
RelativeURL.createObjectURL(new Blob())
// More examples:
const RelativeURL = URLfrom(process.cwd() || import.meta.url || 'file://')
new RelativeURL('./readme.md').toString() // file:///Users/username/Projects/relative-url/readme.md
```
or if you just want to have something minimalistic
```js
const url = (url, base) => new URL(url, new URL(base, import.meta.url))
url('./readme.md').toString() // file:///Users/username/Projects/relative-url/readme.md
```
--
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/531#issuecomment-1372473805
You are receiving this because you are subscribed to this thread.
Message ID: <whatwg/url/issues/531/1372473805@github.com>
Received on Thursday, 5 January 2023 16:55:38 UTC