hyperjump-io / uri

A small and fast library for validating, parsing, and resolving URIs (RFC 3986) and IRIs (RFC 3987).
MIT License
2 stars 1 forks source link

URI

A small and fast library for validating, parsing, and resolving URIs (RFC 3986) and IRIs (RFC 3987).

Install

Designed for node.js (ES Modules, TypeScript) and browsers.

npm install @hyperjump/uri

Usage

import { resolveUri, parseUri, isUri, isIri } from "@hyperjump/uri"

const resolved = resolveUri("foo/bar", "http://example.com/aaa/bbb"); // https://example.com/aaa/foo/bar

const components = parseUri("https://jason@example.com:80/foo?bar#baz"); // {
//   scheme: "https",
//   authority: "jason@example.com:80",
//   userinfo: "jason",
//   host: "example.com",
//   port: "80",
//   path: "/foo",
//   query: "bar",
//   fragment: "baz"
// }

const a = isUri("http://examplé.org/rosé#"); // false
const a = isIri("http://examplé.org/rosé#"); // true

API

Resolve Relative References

These functions resolve relative-references against a base URI/IRI. The base URI/IRI must be absolute, meaning it must have a scheme (https) and no fragment (#foo). The resolution process will normalize the result.

Normalize

These functions apply the following normalization rules.

  1. Decode any unnecessarily percent-encoded characters.
  2. Convert any lowercase characters in the hex numbers of percent-encoded characters to uppercase.
  3. Resolve and remove any dot-segments (/., /..) in paths.
  4. Convert the scheme to lowercase.
  5. Convert the authority to lowercase.

URI

A URI is not relative and may include a fragment.

URI-Reference

A URI-reference may be relative.

absolute-URI

An absolute-URI is not relative an does not include a fragment.

IRI

An IRI is not relative and may include a fragment.

IRI-reference

An IRI-reference may be relative.

absolute-IRI

An absolute-IRI is not relative an does not include a fragment.

Types

Contributing

Tests

Run the tests

npm test

Run the tests with a continuous test runner

npm test -- --watch