shelving 1.161.0 → 1.161.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/store/URLStore.d.ts +3 -4
- package/store/URLStore.js +3 -4
- package/util/path.d.ts +1 -1
- package/util/path.js +3 -3
- package/util/url.d.ts +28 -12
- package/util/url.js +63 -30
package/package.json
CHANGED
package/store/URLStore.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Data } from "../util/data.js";
|
|
2
|
-
import { type
|
|
3
|
-
import { type PossibleURL } from "../util/url.js";
|
|
2
|
+
import { type PossibleURL, type PossibleURLParams, type URLParams } from "../util/url.js";
|
|
4
3
|
import { Store } from "./Store.js";
|
|
5
4
|
/** Store a URL, e.g. `https://top.com/a/b/c` */
|
|
6
5
|
export declare class URLStore extends Store<URL> {
|
|
@@ -21,7 +20,7 @@ export declare class URLStore extends Store<URL> {
|
|
|
21
20
|
/** Get the URL params as a string. */
|
|
22
21
|
get search(): string;
|
|
23
22
|
/** Get the URL params as a dictionary. */
|
|
24
|
-
get params():
|
|
23
|
+
get params(): URLParams;
|
|
25
24
|
/** Return a single param in this URL, or `undefined` if it could not be found. */
|
|
26
25
|
getParam(key: string): string | undefined;
|
|
27
26
|
/** Require a single param in this URL, or throw `RequiredError` if it could not be found. */
|
|
@@ -37,7 +36,7 @@ export declare class URLStore extends Store<URL> {
|
|
|
37
36
|
/** Return the current URL with an additional param. */
|
|
38
37
|
withParam(key: string, value: unknown): URL;
|
|
39
38
|
/** Return the current URL with an additional param. */
|
|
40
|
-
withParams(params:
|
|
39
|
+
withParams(params: PossibleURLParams): URL;
|
|
41
40
|
/** Return the current URL with an additional param. */
|
|
42
41
|
omitParams(...keys: string[]): URL;
|
|
43
42
|
/** Return the current URL with an additional param. */
|
package/store/URLStore.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { getSetter } from "../util/class.js";
|
|
2
|
-
import {
|
|
3
|
-
import { getURL, getURLParam, omitURLParams, requireURL, requireURLParam, withURLParam, withURLParams, } from "../util/url.js";
|
|
1
|
+
import { getGetter, getSetter } from "../util/class.js";
|
|
2
|
+
import { getURL, getURLParam, getURLParams, omitURLParams, requireURL, requireURLParam, withURLParam, withURLParams, } from "../util/url.js";
|
|
4
3
|
import { Store } from "./Store.js";
|
|
5
4
|
/** Store a URL, e.g. `https://top.com/a/b/c` */
|
|
6
5
|
export class URLStore extends Store {
|
|
@@ -52,7 +51,7 @@ export class URLStore extends Store {
|
|
|
52
51
|
}
|
|
53
52
|
/** Get the URL params as a dictionary. */
|
|
54
53
|
get params() {
|
|
55
|
-
return
|
|
54
|
+
return getURLParams(this.value.searchParams, getGetter(this, "params"));
|
|
56
55
|
}
|
|
57
56
|
/** Return a single param in this URL, or `undefined` if it could not be found. */
|
|
58
57
|
getParam(key) {
|
package/util/path.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare function getPath(value: Nullish<string | URL>, base?: AbsolutePat
|
|
|
29
29
|
* @param base Absolute path used for resolving relative paths in `possible`
|
|
30
30
|
* @return Absolute path with a leading trailing slash, e.g. `/a/c/b`
|
|
31
31
|
*/
|
|
32
|
-
export declare function requirePath(value: string, base?: AbsolutePath, caller?: AnyCaller): AbsolutePath;
|
|
32
|
+
export declare function requirePath(value: string | URL, base?: AbsolutePath, caller?: AnyCaller): AbsolutePath;
|
|
33
33
|
/** Is a target path active? */
|
|
34
34
|
export declare function isPathActive(target: AbsolutePath, current: AbsolutePath): boolean;
|
|
35
35
|
/** Is a target path proud (i.e. is the current path, or is a child of the current path)? */
|
package/util/path.js
CHANGED
|
@@ -11,7 +11,7 @@ export function isRelativePath(path) {
|
|
|
11
11
|
function _cleanPath(path) {
|
|
12
12
|
return path
|
|
13
13
|
.replace(/[/\\]+/g, "/") // Normalise slashes.
|
|
14
|
-
.replace(/(?!^)\/$/g, ""); //
|
|
14
|
+
.replace(/(?!^)\/$/g, ""); // Trim trailing slashes.
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Resolve a relative or absolute path and return the absolute path, or `undefined` if not a valid path.
|
|
@@ -22,8 +22,8 @@ function _cleanPath(path) {
|
|
|
22
22
|
* @param base Absolute path used for resolving relative paths in `possible`
|
|
23
23
|
* @return Absolute path with a leading trailing slash, e.g. `/a/c/b`
|
|
24
24
|
*/
|
|
25
|
-
export function getPath(value, base
|
|
26
|
-
const url = getURL(value, `http://j.com${base
|
|
25
|
+
export function getPath(value, base) {
|
|
26
|
+
const url = getURL(value, base ? `http://j.com${base}${base.endsWith("/") ? "" : "/"}` : "http://j.com");
|
|
27
27
|
if (url) {
|
|
28
28
|
const { pathname, search, hash } = url;
|
|
29
29
|
if (isAbsolutePath(pathname))
|
package/util/url.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type { ImmutableDictionary } from "./dictionary.js";
|
|
1
|
+
import { type Data } from "./data.js";
|
|
2
|
+
import type { DictionaryItem, ImmutableDictionary } from "./dictionary.js";
|
|
3
3
|
import type { AnyCaller } from "./function.js";
|
|
4
4
|
import { type Nullish } from "./null.js";
|
|
5
5
|
/** Values that can be converted to a URL instance. */
|
|
@@ -12,19 +12,35 @@ export declare function assertURL(value: unknown, caller?: AnyCaller): asserts v
|
|
|
12
12
|
export declare function getURL(possible: Nullish<PossibleURL>, base?: PossibleURL | undefined): URL | undefined;
|
|
13
13
|
/** Convert a possible URL to a URL, or throw `RequiredError` if conversion fails. */
|
|
14
14
|
export declare function requireURL(possible: PossibleURL, base?: PossibleURL, caller?: AnyCaller): URL;
|
|
15
|
+
/** Type for a set of named URL parameters. */
|
|
16
|
+
export type URLParams = ImmutableDictionary<string>;
|
|
17
|
+
/** Type for things that can be converted to named URL parameters. */
|
|
18
|
+
export type PossibleURLParams = PossibleURL | URLSearchParams | Data | Iterable<DictionaryItem<unknown>>;
|
|
15
19
|
/**
|
|
16
|
-
* Get
|
|
17
|
-
*
|
|
20
|
+
* Get a set of entries for a set of possible URL params.
|
|
21
|
+
*
|
|
22
|
+
* Note: Not as simple as just converting with `Object.fromEntries()`:
|
|
23
|
+
* 1. When `URLSearchParams` contains multiple values for the same key, calling `params.get()` will return the _first_ value.
|
|
24
|
+
* 2. So when converting this to a simple data object, only one value per key can be represented, but it needs to be the _first_ one.
|
|
25
|
+
* 3. Since we're looping through anyway, we also take the time to convert values to strings, so we can accept a wider range of input types.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getURLEntries(input: PossibleURLParams, caller?: AnyCaller): Iterable<DictionaryItem<string>>;
|
|
28
|
+
/** Get a set of params for a URL as a dictionary. */
|
|
29
|
+
export declare function getURLParams(input: PossibleURLParams, caller?: AnyCaller): URLParams;
|
|
30
|
+
/** Get a single named param from a URL. */
|
|
31
|
+
export declare function getURLParam(input: PossibleURLParams, key: string): string | undefined;
|
|
32
|
+
/** Get a single named param from a URL. */
|
|
33
|
+
export declare function requireURLParam(input: PossibleURLParams, key: string, caller?: AnyCaller): string;
|
|
34
|
+
/**
|
|
35
|
+
* Return a URL with a new param set (or same URL if no changes were made).
|
|
36
|
+
* - Throws `ValueError` if the value could not be converted to a string.
|
|
18
37
|
*/
|
|
19
|
-
export declare function getURLParams(params: URLSearchParams): ImmutableDictionary<string>;
|
|
20
|
-
/** Get a param from a URL. */
|
|
21
|
-
export declare function getURLParam(params: URLSearchParams, key: string): string | undefined;
|
|
22
|
-
/** Get a param from a URL. */
|
|
23
|
-
export declare function requireURLParam(params: URLSearchParams, key: string, caller?: AnyCaller): string | undefined;
|
|
24
|
-
/** Return a URL with a new param set (or same URL if no changes were made). */
|
|
25
38
|
export declare function withURLParam(url: PossibleURL, key: string, value: unknown, caller?: AnyCaller): URL;
|
|
26
|
-
/**
|
|
27
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Return a URL with several new params set (or same URL if no changes were made).
|
|
41
|
+
* - Throws `ValueError` if any of the values could not be converted to strings.
|
|
42
|
+
*/
|
|
43
|
+
export declare function withURLParams(url: PossibleURL, params: PossibleURLParams, caller?: AnyCaller): URL;
|
|
28
44
|
/** Return a URL without one or more params (or same URL if no changes were made). */
|
|
29
45
|
export declare function omitURLParams(url: PossibleURL, ...keys: string[]): URL;
|
|
30
46
|
/** Return a URL without a param (or same URL if no changes were made). */
|
package/util/url.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { RequiredError } from "../error/RequiredError.js";
|
|
2
|
+
import { ValueError } from "../error/ValueError.js";
|
|
3
|
+
import { isData } from "./data.js";
|
|
2
4
|
import { notNullish } from "./null.js";
|
|
3
5
|
import { getProps } from "./object.js";
|
|
4
|
-
import {
|
|
6
|
+
import { getString, isString } from "./string.js";
|
|
5
7
|
/** Is an unknown value a URL object? */
|
|
6
8
|
export function isURL(value) {
|
|
7
9
|
return value instanceof URL;
|
|
@@ -13,14 +15,8 @@ export function assertURL(value, caller = assertURL) {
|
|
|
13
15
|
}
|
|
14
16
|
/** Convert a possible URL to a URL, or return `undefined` if conversion fails. */
|
|
15
17
|
export function getURL(possible, base = _BASE) {
|
|
16
|
-
if (notNullish(possible))
|
|
17
|
-
|
|
18
|
-
return isURL(possible) ? possible : new URL(possible, base);
|
|
19
|
-
}
|
|
20
|
-
catch (_e) {
|
|
21
|
-
//
|
|
22
|
-
}
|
|
23
|
-
}
|
|
18
|
+
if (notNullish(possible))
|
|
19
|
+
return isURL(possible) ? possible : URL.parse(possible, base) || undefined;
|
|
24
20
|
}
|
|
25
21
|
const _BASE = typeof document === "object" ? document.baseURI : undefined;
|
|
26
22
|
/** Convert a possible URL to a URL, or throw `RequiredError` if conversion fails. */
|
|
@@ -30,46 +26,83 @@ export function requireURL(possible, base, caller = requireURL) {
|
|
|
30
26
|
return url;
|
|
31
27
|
}
|
|
32
28
|
/**
|
|
33
|
-
* Get
|
|
34
|
-
*
|
|
29
|
+
* Get a set of entries for a set of possible URL params.
|
|
30
|
+
*
|
|
31
|
+
* Note: Not as simple as just converting with `Object.fromEntries()`:
|
|
32
|
+
* 1. When `URLSearchParams` contains multiple values for the same key, calling `params.get()` will return the _first_ value.
|
|
33
|
+
* 2. So when converting this to a simple data object, only one value per key can be represented, but it needs to be the _first_ one.
|
|
34
|
+
* 3. Since we're looping through anyway, we also take the time to convert values to strings, so we can accept a wider range of input types.
|
|
35
35
|
*/
|
|
36
|
-
export function getURLParams
|
|
36
|
+
export function* getURLEntries(input, caller = getURLParams) {
|
|
37
|
+
if (input instanceof URLSearchParams) {
|
|
38
|
+
yield* input;
|
|
39
|
+
}
|
|
40
|
+
else if (isString(input) || isURL(input)) {
|
|
41
|
+
yield* requireURL(input, undefined, caller).searchParams;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const done = [];
|
|
45
|
+
for (const [key, value] of getProps(input)) {
|
|
46
|
+
if (done.includes(key))
|
|
47
|
+
continue;
|
|
48
|
+
done.push(key);
|
|
49
|
+
const str = getString(value);
|
|
50
|
+
if (str === undefined)
|
|
51
|
+
throw new ValueError(`URL param "${key}" must be string`, { received: value, caller });
|
|
52
|
+
yield [key, str];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Get a set of params for a URL as a dictionary. */
|
|
57
|
+
export function getURLParams(input, caller = getURLParams) {
|
|
37
58
|
const output = {};
|
|
38
|
-
for (const [key,
|
|
39
|
-
|
|
40
|
-
output[key] = value;
|
|
59
|
+
for (const [key, str] of getURLEntries(input, caller))
|
|
60
|
+
output[key] = str;
|
|
41
61
|
return output;
|
|
42
62
|
}
|
|
43
|
-
/** Get a param from a URL. */
|
|
44
|
-
export function getURLParam(
|
|
45
|
-
|
|
63
|
+
/** Get a single named param from a URL. */
|
|
64
|
+
export function getURLParam(input, key) {
|
|
65
|
+
if (input instanceof URLSearchParams)
|
|
66
|
+
return input.get(key) || undefined;
|
|
67
|
+
if (isData(input))
|
|
68
|
+
return getString(input[key]);
|
|
69
|
+
return getURLParams(input)[key];
|
|
46
70
|
}
|
|
47
|
-
/** Get a param from a URL. */
|
|
48
|
-
export function requireURLParam(
|
|
49
|
-
const value =
|
|
50
|
-
if (value ===
|
|
51
|
-
throw new RequiredError(`URL param "${key}" is required`, { received:
|
|
71
|
+
/** Get a single named param from a URL. */
|
|
72
|
+
export function requireURLParam(input, key, caller = requireURLParam) {
|
|
73
|
+
const value = getURLParam(input, key);
|
|
74
|
+
if (value === undefined)
|
|
75
|
+
throw new RequiredError(`URL param "${key}" is required`, { received: input, caller });
|
|
52
76
|
return value;
|
|
53
77
|
}
|
|
54
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Return a URL with a new param set (or same URL if no changes were made).
|
|
80
|
+
* - Throws `ValueError` if the value could not be converted to a string.
|
|
81
|
+
*/
|
|
55
82
|
export function withURLParam(url, key, value, caller = withURLParam) {
|
|
56
83
|
const input = requireURL(url);
|
|
57
84
|
const output = new URL(input);
|
|
58
|
-
|
|
85
|
+
const str = getString(value);
|
|
86
|
+
if (str === undefined)
|
|
87
|
+
throw new ValueError(`URL param "${key}" must be string`, { received: value, caller });
|
|
88
|
+
output.searchParams.set(key, str);
|
|
59
89
|
return input.href === output.href ? input : output;
|
|
60
90
|
}
|
|
61
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* Return a URL with several new params set (or same URL if no changes were made).
|
|
93
|
+
* - Throws `ValueError` if any of the values could not be converted to strings.
|
|
94
|
+
*/
|
|
62
95
|
export function withURLParams(url, params, caller = withURLParams) {
|
|
63
96
|
const input = requireURL(url);
|
|
64
|
-
const output =
|
|
65
|
-
for (const [key,
|
|
66
|
-
output.searchParams.set(key,
|
|
97
|
+
const output = new URL(input);
|
|
98
|
+
for (const [key, str] of getURLEntries(params, caller))
|
|
99
|
+
output.searchParams.set(key, str);
|
|
67
100
|
return input.href === output.href ? input : output;
|
|
68
101
|
}
|
|
69
102
|
/** Return a URL without one or more params (or same URL if no changes were made). */
|
|
70
103
|
export function omitURLParams(url, ...keys) {
|
|
71
104
|
const input = requireURL(url);
|
|
72
|
-
const output =
|
|
105
|
+
const output = new URL(input);
|
|
73
106
|
for (const key of keys)
|
|
74
107
|
output.searchParams.delete(key);
|
|
75
108
|
return input.href === output.href ? input : output;
|