@hyperjump/uri
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -17,4 +17,8 @@ export const isUri: (value: string) => boolean; | ||
export const resolveReference: (uriReference: string, baseUri: string, type?: "uri" | "iri") => string; | ||
export const toAbsoluteUri: (value: string) => string; | ||
export const toAbsoluteIri: (value: string) => string; | ||
export const resolveUri: (uriReference: string, baseUri: string) => string; | ||
export const resolveIri: (iriReference: string, baseIri: string) => string; | ||
export type IdentifierComponents = { | ||
@@ -21,0 +25,0 @@ scheme: string; |
@@ -86,2 +86,21 @@ // Common | ||
const toAbsolute = (parse) => (identifier) => { | ||
const components = parse(identifier); | ||
delete components.fragment; | ||
return composeIdentifier(components); | ||
}; | ||
export const toAbsoluteUri = toAbsolute(parseUri); | ||
export const toAbsoluteIri = toAbsolute(parseIri); | ||
const composeIdentifier = (components) => { | ||
let resolved = components.scheme + ":"; | ||
resolved += components.authority === undefined ? "" : "//" + components.authority; | ||
resolved += components.path; | ||
resolved += components.query === undefined ? "" : "?" + components.query; | ||
resolved += components.fragment === undefined ? "" : "#" + components.fragment; | ||
return resolved; | ||
}; | ||
const parse = { | ||
@@ -92,20 +111,20 @@ uri: { base: parseAbsoluteUri, reference: parseUriReference }, | ||
export const resolveReference = (uriReference, baseUri, type = "uri") => { | ||
let { scheme, authority, path, query, fragment } = parse[type].reference(uriReference); | ||
const resolveReference = (type) => (reference, base) => { | ||
const resolvedComponents = parse[type].reference(reference); | ||
if (scheme === undefined) { | ||
const base = parse[type].base(baseUri); | ||
scheme = base.scheme; | ||
if (resolvedComponents.scheme === undefined) { | ||
const baseComponents = parse[type].base(base); | ||
resolvedComponents.scheme = baseComponents.scheme; | ||
if (authority === undefined) { | ||
authority = base.authority; | ||
if (resolvedComponents.authority === undefined) { | ||
resolvedComponents.authority = baseComponents.authority; | ||
if (path === "") { | ||
path = base.path; | ||
if (resolvedComponents.path === "") { | ||
resolvedComponents.path = baseComponents.path; | ||
if (query === undefined) { | ||
query = base.query; | ||
if (resolvedComponents.query === undefined) { | ||
resolvedComponents.query = baseComponents.query; | ||
} | ||
} else if (!path.startsWith("/")) { | ||
path = mergePaths(path, base); | ||
} else if (!resolvedComponents.path.startsWith("/")) { | ||
resolvedComponents.path = mergePaths(resolvedComponents.path, baseComponents); | ||
} | ||
@@ -115,10 +134,10 @@ } | ||
let resolved = scheme + ":"; | ||
resolved += authority === undefined ? "" : "//" + authority; | ||
resolved += removeDotSegments(path); | ||
resolved += query === undefined ? "" : "?" + query; | ||
resolved += fragment === undefined ? "" : "#" + fragment; | ||
return resolved; | ||
resolvedComponents.path = removeDotSegments(resolvedComponents.path); | ||
return composeIdentifier(resolvedComponents); | ||
}; | ||
export const resolveUri = resolveReference("uri"); | ||
export const resolveIri = resolveReference("iri"); | ||
const mergePaths = (path, base) => { | ||
@@ -125,0 +144,0 @@ if (base.authority && base.path === "") { |
{ | ||
"name": "@hyperjump/uri", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A small and fast library for validating parsing and resolving URIs and IRIs", | ||
"type": "module", | ||
"main": "./lib/index.js", | ||
"exports": "./lib/index.js", | ||
@@ -7,0 +8,0 @@ "scripts": { |
@@ -15,5 +15,5 @@ # URI | ||
```javascript | ||
import { resolveReference, parseUri, isUri, isIri } from "@hyperjump/uri" | ||
import { resolveUri, parseUri, isUri, isIri } from "@hyperjump/uri" | ||
const resolved = resolveReference("foo/bar", "http://example.com/aaa/bbb"); // https://example.com/aaa/foo/bar | ||
const resolved = resolveUri("foo/bar", "http://example.com/aaa/bbb"); // https://example.com/aaa/foo/bar | ||
@@ -37,9 +37,11 @@ const components = parseUri("https://jason@example.com:80/foo?bar#baz"); // { | ||
### Resolution | ||
* **resolveReference**: (uriReference: string, baseUri: string, type?: "uri" | "iri" = "uri") => string; | ||
* **resolveUri**: (uriReference: string, baseUri: string) => string | ||
Resolve a URI-reference against a base URI. Or, resolve an IRI-reference | ||
against a base IRI. Use the `type` parameter with "iri" to resolve IRIs. The | ||
`baseUri` must be an | ||
Resolve a URI-reference against a base URI. The `baseUri` must be an | ||
[absolute-URI](https://www.rfc-editor.org/rfc/rfc3986#section-4.3). | ||
* **resolveIri**: (iriReference: string, baseIri: string) => string | ||
Resolve a IRI-reference against a base IRI. The `baseIri` must be an | ||
absolute-IRI. | ||
### URI | ||
@@ -49,5 +51,8 @@ A [URI](https://www.rfc-editor.org/rfc/rfc3986#section-3) is not relative and | ||
* **isUri**: (value: string) => boolean; | ||
* **parseUri**: (value: string) => IdentifierComponents; | ||
* **isUri**: (value: string) => boolean | ||
* **parseUri**: (value: string) => IdentifierComponents | ||
* **toAbsoluteUri**: (value: string) => string | ||
Takes a URI and strips its fragment component if it exists. | ||
### URI-Reference | ||
@@ -57,4 +62,4 @@ A [URI-reference](https://www.rfc-editor.org/rfc/rfc3986#section-4.1) may be | ||
* **isUriReference**: (value: string) => boolean; | ||
* **parseUriReference**: (value: string) => IdentifierComponents; | ||
* **isUriReference**: (value: string) => boolean | ||
* **parseUriReference**: (value: string) => IdentifierComponents | ||
@@ -65,4 +70,4 @@ ### absolute-URI | ||
* **isAbsoluteUri**: (value: string) => boolean; | ||
* **parseAbsoluteUri**: (value: string) => IdentifierComponents; | ||
* **isAbsoluteUri**: (value: string) => boolean | ||
* **parseAbsoluteUri**: (value: string) => IdentifierComponents | ||
@@ -72,10 +77,13 @@ ### IRI | ||
* **isIri**: (value: string) => boolean; | ||
* **parseIri**: (value: string) => IdentifierComponents; | ||
* **isIri**: (value: string) => boolean | ||
* **parseIri**: (value: string) => IdentifierComponents | ||
* **toAbsoluteUri**: (value: string) => string | ||
Takes an IRI and strips its fragment component if it exists. | ||
### IRI-reference | ||
A IRI-reference may be relative. | ||
An IRI-reference may be relative. | ||
* **isIriReference**: (value: string) => boolean; | ||
* **parseIriReference**: (value: string) => IdentifierComponents; | ||
* **isIriReference**: (value: string) => boolean | ||
* **parseIriReference**: (value: string) => IdentifierComponents | ||
@@ -85,4 +93,4 @@ ### absolute-IRI | ||
* **isAbsoluteIri**: (value: string) => boolean; | ||
* **parseAbsoluteIri**: (value: string) => IdentifierComponents; | ||
* **isAbsoluteIri**: (value: string) => boolean | ||
* **parseAbsoluteIri**: (value: string) => IdentifierComponents | ||
@@ -89,0 +97,0 @@ ### Types |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12869
184
112