Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Feb 18, 2022
1 parent e6713f5 commit a6d0649
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "relative-url-interface",
"description": "Create URL interfaces without a base URL",
"version": "0.1.0",
"version": "0.2.0",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -61,12 +61,12 @@
"homepage": "https://github.com/astro-community/relative-url#readme",
"devDependencies": {
"@rollup/plugin-commonjs": "21.0.1",
"@rollup/plugin-inject": "4.0.3",
"@rollup/plugin-node-resolve": "13.1.1",
"@rollup/plugin-inject": "4.0.4",
"@rollup/plugin-node-resolve": "13.1.3",
"@rollup/plugin-typescript": "8.3.0",
"magic-string": "0.25.7",
"rollup": "2.61.1",
"spec-url": "2.0.0-dev.1"
"rollup": "2.67.2",
"spec-url": "^2.3.0-dev"
},
"scripts": {
"build": "node build.js",
Expand Down
39 changes: 21 additions & 18 deletions src/RelativeURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface InternalURL {
scheme?: string
user?: string
pass?: string
host?: string
host?: string | number | number[] | string[]
port?: string
drive?: string
root?: string
Expand All @@ -18,30 +18,33 @@ const mapOfInternalURLs: WeakMap<URL, InternalURL> = new WeakMap

const getInternalURL = (instance: URL) => mapOfInternalURLs.get(instance) || mapOfInternalURLs.set(instance, {}).get(instance) as InternalURL

const toInternalURL = (...hrefs: (URL | string)[]): InternalURL => url.normalise(
hrefs.reduce(
(internalURL, nextHREF) => url.goto(
internalURL,
url.parse(
String(nextHREF)
)
),
// @ts-ignore
const toInternalURL = (...hrefs: (URL | string)[]): InternalURL => hrefs.reduce(
(internalURL, nextHREF) => url.rebase(
url.parse(
String(
hrefs.shift()
)
String(nextHREF)
),
internalURL
),
url.parse(
String(
hrefs.shift()
)
)
)

const toHost = (url: number | string | number[] | string[] | void) => (
url == null ? '' : typeof url == 'object' ? url.join('') : String(url)
)

export class RelativeURL extends URL {
constructor(href: URL | string, ...hrefs: (URL | string)[]) {
super('noop://')

const internalURL = toInternalURL(...[ href ].concat(hrefs).reverse())

super.hash = internalURL.hash || ''
super.hostname = internalURL.host || ''
super.hostname = toHost(internalURL.host)
super.search = internalURL.query || ''

mapOfInternalURLs.set(this, internalURL)
Expand All @@ -59,7 +62,7 @@ export class RelativeURL extends URL {
const internalURL = getInternalURL(this)

return String().concat(
internalURL.host ?? '',
toHost(internalURL.host),
internalURL.port != null ? ':' + internalURL.port : ''
)
}
Expand All @@ -71,8 +74,8 @@ export class RelativeURL extends URL {
internalURL.scheme ? internalURL.scheme + ':' : '',
internalURL.user ? '//' + internalURL.user : '',
internalURL.pass ? ':' + internalURL.pass : '',
internalURL.host ? internalURL.user ? '@' : '//' : '',
internalURL.host ?? '',
toHost(internalURL.host) ? internalURL.user ? '@' : '//' : '',
toHost(internalURL.host),
internalURL.port != null ? ':' + internalURL.port : '',
internalURL.drive ? '/' + internalURL.drive : '',
internalURL.root ?? '',
Expand All @@ -87,7 +90,7 @@ export class RelativeURL extends URL {
const internalURL = toInternalURL(href)

super.hash = internalURL.hash || ''
super.hostname = internalURL.host || ''
super.hostname = toHost(internalURL.host)
super.password = internalURL.pass || ''
super.search = internalURL.query || ''
super.username = internalURL.user || ''
Expand All @@ -96,7 +99,7 @@ export class RelativeURL extends URL {
}

get hostname(): string {
return getInternalURL(this).host ?? ''
return toHost(getInternalURL(this).host)
}

set hostname(hostname: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/spec-url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare module 'spec-url' {

export function goto(url1: string | URL, url2: string | URL): SpecURL

export function rebase(url1: string | URL, url2: string | URL): SpecURL

class SpecURL extends URL {
resolve(url: string | URL): SpecURL
}
Expand Down

0 comments on commit a6d0649

Please sign in to comment.