UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

113 lines (112 loc) 3.71 kB
import { getGetter, getSetter } from "../util/class.js"; import { clearURIParams, getURIParam, getURIParams, omitURIParams, requireURIParam, withURIParam, withURIParams, } from "../util/uri.js"; import { getURL, requireURL } from "../util/url.js"; import { Store } from "./Store.js"; /** Store a URL, e.g. `https://top.com/a/b/c` */ export class URLStore extends Store { base; constructor(url, base) { const baseURL = getURL(base); super(requireURL(url, baseURL)); this.base = baseURL; } set value(url) { super.value = requireURL(url, this.base, getSetter(this, "value")); } get value() { return super.value; } get href() { return this.value.href; } set href(href) { this.value = requireURL(href, this.base, getSetter(this, "href")); } get origin() { return this.value.origin; } get protocol() { return this.value.protocol; } get username() { return this.value.username; } get password() { return this.value.password; } get hostname() { return this.value.hostname; } get host() { return this.value.host; } get port() { return this.value.port; } get pathname() { return this.value.pathname; } /** Get the URL params as a string. */ get search() { return this.value.search; } /** Get the URL params as a dictionary. */ get params() { return getURIParams(this.value.searchParams, getGetter(this, "params")); } /** Return a single param in this URL, or `undefined` if it could not be found. */ getParam(key) { return getURIParam(this.value.searchParams, key); } /** Require a single param in this URL, or throw `RequiredError` if it could not be found. */ requireParam(key) { return requireURIParam(this.value.searchParams, key, getSetter(this, "requireParam")); } /** Set all params in this URL (all current params are cleared). */ setParams(params) { this.value = withURIParams(clearURIParams(this.value, this.setParams), params, this.setParams); } /** Set a single named param in this URL. */ setParam(key, value) { this.value = withURIParam(this.value, key, value, this.setParam); } /** Update several params in this URL (merged with current params). */ updateParams(params) { this.value = withURIParams(this.value, params, this.updateParams); } /** Delete one or more params in this URL. */ deleteParam(key, ...keys) { this.value = omitURIParams(this.value, key, ...keys); } /** Delete one or more params in this URL. */ deleteParams(key, ...keys) { this.value = omitURIParams(this.value, key, ...keys); } /** Clear all params from this URL. */ clearParams() { this.value = clearURIParams(this.value, this.clearParams); } /** Return the current URL with an additional param. */ withParam(key, value) { return withURIParam(this.value, key, value, this.withParam); } /** Return the current URL with an additional param. */ withParams(params) { return withURIParams(this.value, params, this.withParams); } /** Return the current URL with an additional param. */ omitParams(...keys) { return omitURIParams(this.value, ...keys); } /** Return the current URL with an additional param. */ omitParam(key) { return omitURIParams(this.value, key); } // Override `equal()` to just compare the hrefs. equal(a, b) { return a.href === b.href; } toString() { return this.href; } }