/relative-url

Create URL interfaces without a base URL.

Primary LanguageJavaScript

RelativeURL

RelativeURL creates fully compatible URL interfaces, without requiring a base URL. 1 2 3

npm install relative-url-interface
const kitten_image = new RelativeURL('../assets/kitten.jpg')

String(kitten_image) // "../assets/kitten.jpg"

The RelativeURL interface extends URL and can be used as a drop-in replacement. It is powered by spec-url and is ideal for server-side code, pre-compiled code, or any situation where an absolute URL may not be known.

const puppy_page = new RelativeURL('file://path/to/site/src/pages/puppy.astro')

const absolute_kitten = new RelativeURL(kitten_image, puppy_page)

fs.readFile(absolute_kitten, 'utf8') // reads "file://path/to/site/src/assets/kitten.jpg"

Features

hash

The hash property is a string representing the fragment identifier of the URL. When present, it is preceeded by a number sign (#).

host

The host property is a string representing the domain and port of the URL. When present, the port is preceeded by a colon (:).

hostname

The hostname property is a string representing the domain of the URL.

href

The href property is a string representing the whole URL, including any search parameters and fragment identifiers.

origin

The origin property is a string representing the scheme, domain, and port of the URL. When present, the port is preceeded by a colon (:).

password

The password property is a string representing the password of the URL.

pathname

The pathname property is a string representing the path of the URL, which does not include the origin, search parameters, or fragment identifiers.

port

The port property is a string representing the port of the URL.

protocol

The protocol property is a string representing the scheme of the URL, including the colon (:) that proceeds it.

search

The search property is a string representing the search parameters of the URL. When present, it is preceeded by a question mark (?).

searchParams

The searchParams property is a URLSearchParams object representing the parsed search parameters of the URL.

username

The username property is a string representing the username of the URL.

segments

The segments property is an array of strings representing the path segments of the URL.

Methods

toString

The toString method returns the whole URL as a string. It is a synonym for the href getter property.

new RelativeURL('../assets/kitten.jpg').toString() // "../assets/kitten.jpg"

toJSON

The toJSON method returns the whole URL as a string. It is a synonym for the href getter property.

new RelativeURL('../assets/kitten.jpg').toJSON() // "../assets/kitten.jpg"

to

The to method returns a new URL resolved by the current URL.

const kitten = new RelativeURL('../assets/kitten.jpg')

String(kitten.to('puppy.jpg')) // "../assets/puppy.jpg"

License

Code original to this project is licensed under the CC0-1.0 License.

Code from spec-url is licensed under the The MIT License (MIT), Copyright Alwin Blok.