shelving 1.169.0 → 1.171.0

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/README.md CHANGED
@@ -37,7 +37,7 @@ Shelving does not include code for CommonJS `require()` imports, so using it in
37
37
 
38
38
  ## Modules
39
39
 
40
- Shelving is created from small individual modules which can be imported individually (using e.g. `import { addProp } from "shelving/object`). Modules marked with `✅` are also re-exported from the main `"shelving"` module.
40
+ Shelving is created from small individual modules which can be imported individually (using e.g. `import { addProp } from "shelving/util/object`). Modules marked with `✅` are also re-exported from the main `"shelving"` module.
41
41
 
42
42
  @todo Write these docs!
43
43
 
@@ -11,11 +11,11 @@ export const WORD_CONTENT_REGEXP = "[\\p{L}\\p{N}]+"; // Word content (at least
11
11
  export const WORD_START_REGEXP = "(?<![\\p{L}\\p{N}])"; // Start of word (previous character is not a letter or number).
12
12
  export const WORD_END_REGEXP = "(?![\\p{L}\\p{N}])"; // End of word (next character is not a letter or number).
13
13
  export function getBlockRegExp(content, start = BLOCK_START_REGEXP, end = BLOCK_END_REGEXP) {
14
- return new RegExp(`${start}${getRegExpSource(content)}${end}`);
14
+ return new RegExp(`${start}${getRegExpSource(content)}${end}`, "u");
15
15
  }
16
16
  export function getLineRegExp(content = LINE_CONTENT_REGEXP, end = LINE_END_REGEXP, start = LINE_START_REGEXP) {
17
- return new RegExp(`${start}${getRegExpSource(content)}${end}`);
17
+ return new RegExp(`${start}${getRegExpSource(content)}${end}`, "u");
18
18
  }
19
19
  export function getWordRegExp(content = WORD_CONTENT_REGEXP, start = WORD_START_REGEXP, end = WORD_END_REGEXP) {
20
- return new RegExp(`${start}${getRegExpSource(content)}${end}`);
20
+ return new RegExp(`${start}${getRegExpSource(content)}${end}`, "u");
21
21
  }
package/package.json CHANGED
@@ -11,8 +11,11 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.169.0",
15
- "repository": "https://github.com/dhoulb/shelving",
14
+ "version": "1.171.0",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/dhoulb/shelving.git"
18
+ },
16
19
  "author": "Dave Houlbrooke <dave@shax.com>",
17
20
  "license": "0BSD",
18
21
  "type": "module",
@@ -37,6 +37,8 @@ export declare abstract class Schema<T = unknown> implements Validator<T> {
37
37
  /** Every schema must implement a `validate()` method. */
38
38
  abstract validate(unsafeValue: unknown): T;
39
39
  }
40
+ /** Extract the type from a schema. */
41
+ export type SchemaType<X> = X extends Schema<infer Y> ? Y : never;
40
42
  /** A set of named schemas in `{ name: schema }` format. */
41
43
  export type Schemas<T extends Data = Data> = {
42
44
  readonly [K in keyof T]: Schema<T[K]>;
@@ -25,14 +25,18 @@ export declare class URLStore extends Store<URL> {
25
25
  getParam(key: string): string | undefined;
26
26
  /** Require a single param in this URL, or throw `RequiredError` if it could not be found. */
27
27
  requireParam(key: string): string | undefined;
28
- /** Update a single param in this URL. */
29
- setParam(key: string, value: unknown): void;
30
- /** Update several params in this URL. */
28
+ /** Set all params in this URL (all current params are cleared). */
31
29
  setParams(params: PossibleURIParams): void;
30
+ /** Set a single named param in this URL. */
31
+ setParam(key: string, value: unknown): void;
32
+ /** Update several params in this URL (merged with current params). */
33
+ updateParams(params: PossibleURIParams): void;
32
34
  /** Delete one or more params in this URL. */
33
35
  deleteParam(key: string, ...keys: string[]): void;
34
36
  /** Delete one or more params in this URL. */
35
37
  deleteParams(key: string, ...keys: string[]): void;
38
+ /** Clear all params from this URL. */
39
+ clearParams(): void;
36
40
  /** Return the current URL with an additional param. */
37
41
  withParam(key: string, value: unknown): URL;
38
42
  /** Return the current URL with an additional param. */
@@ -41,5 +45,6 @@ export declare class URLStore extends Store<URL> {
41
45
  omitParams(...keys: string[]): URL;
42
46
  /** Return the current URL with an additional param. */
43
47
  omitParam(key: string): URL;
48
+ equal(a: URL, b: URL): boolean;
44
49
  toString(): string;
45
50
  }
package/store/URLStore.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getGetter, getSetter } from "../util/class.js";
2
- import { getURIParam, getURIParams, omitURIParams, requireURIParam, withURIParam, withURIParams, } from "../util/uri.js";
2
+ import { clearURIParams, getURIParam, getURIParams, omitURIParams, requireURIParam, withURIParam, withURIParams, } from "../util/uri.js";
3
3
  import { getURL, requireURL } from "../util/url.js";
4
4
  import { Store } from "./Store.js";
5
5
  /** Store a URL, e.g. `https://top.com/a/b/c` */
@@ -62,13 +62,17 @@ export class URLStore extends Store {
62
62
  requireParam(key) {
63
63
  return requireURIParam(this.value.searchParams, key, getSetter(this, "requireParam"));
64
64
  }
65
- /** Update a single param in this URL. */
65
+ /** Set all params in this URL (all current params are cleared). */
66
+ setParams(params) {
67
+ this.value = withURIParams(clearURIParams(this.value, this.setParams), params, this.setParams);
68
+ }
69
+ /** Set a single named param in this URL. */
66
70
  setParam(key, value) {
67
- this.value = withURIParam(this.value, key, value, this.setParams);
71
+ this.value = withURIParam(this.value, key, value, this.setParam);
68
72
  }
69
- /** Update several params in this URL. */
70
- setParams(params) {
71
- this.value = withURIParams(this.value, params, this.setParams);
73
+ /** Update several params in this URL (merged with current params). */
74
+ updateParams(params) {
75
+ this.value = withURIParams(this.value, params, this.updateParams);
72
76
  }
73
77
  /** Delete one or more params in this URL. */
74
78
  deleteParam(key, ...keys) {
@@ -78,6 +82,10 @@ export class URLStore extends Store {
78
82
  deleteParams(key, ...keys) {
79
83
  this.value = omitURIParams(this.value, key, ...keys);
80
84
  }
85
+ /** Clear all params from this URL. */
86
+ clearParams() {
87
+ this.value = clearURIParams(this.value, this.clearParams);
88
+ }
81
89
  /** Return the current URL with an additional param. */
82
90
  withParam(key, value) {
83
91
  return withURIParam(this.value, key, value, this.withParam);
@@ -94,6 +102,10 @@ export class URLStore extends Store {
94
102
  omitParam(key) {
95
103
  return omitURIParams(this.value, key);
96
104
  }
105
+ // Override `equal()` to just compare the hrefs.
106
+ equal(a, b) {
107
+ return a.href === b.href;
108
+ }
97
109
  toString() {
98
110
  return this.href;
99
111
  }
package/util/uri.d.ts CHANGED
@@ -63,7 +63,10 @@ export declare function requireURIString(possible: PossibleURI, caller?: AnyCall
63
63
  export type URIParams = ImmutableDictionary<string>;
64
64
  /** Type for things that can be converted to named URI parameters. */
65
65
  export type PossibleURIParams = PossibleURI | URLSearchParams | ImmutableDictionary<unknown>;
66
- /** Get a set of params for a URI as a dictionary. */
66
+ /**
67
+ * Get a set of params for a URI as a dictionary.
68
+ * - Any params with `undefined` value will be ignored.
69
+ */
67
70
  export declare function getURIParams(input: PossibleURIParams, caller?: AnyCaller): URIParams;
68
71
  /** Get a single named param from a URI. */
69
72
  export declare function getURIParam(input: PossibleURIParams, key: string): string | undefined;
@@ -71,13 +74,17 @@ export declare function getURIParam(input: PossibleURIParams, key: string): stri
71
74
  export declare function requireURIParam(input: PossibleURIParams, key: string, caller?: AnyCaller): string;
72
75
  /**
73
76
  * Return a URI with a new param set (or same URI if no changes were made).
74
- * - Throws `ValueError` if the value could not be converted to a string.
77
+ * - Any params with `undefined` value will be ignored.
78
+ *
79
+ * @throws `ValueError` if the value could not be converted to a string.
75
80
  */
76
81
  export declare function withURIParam(url: URL | URLString, key: string, value: unknown, caller?: AnyCaller): URL;
77
82
  export declare function withURIParam(url: PossibleURI, key: string, value: unknown, caller?: AnyCaller): URI;
78
83
  /**
79
84
  * Return a URI with several new params set (or same URI if no changes were made).
80
- * - Throws `ValueError` if any of the values could not be converted to strings.
85
+ * - Param with `undefined` value will be ignored.
86
+ *
87
+ * @throws `ValueError` if any of the values could not be converted to strings.
81
88
  */
82
89
  export declare function withURIParams(url: URL | URLString, params: PossibleURIParams, caller?: AnyCaller): URL;
83
90
  export declare function withURIParams(url: PossibleURI, params: PossibleURIParams, caller?: AnyCaller): URI;
@@ -86,6 +93,9 @@ export declare function omitURIParams(url: URL | URLString, ...keys: string[]):
86
93
  export declare function omitURIParams(url: PossibleURI, ...keys: string[]): URI;
87
94
  /** Return a URI without a param (or same URI if no changes were made). */
88
95
  export declare const omitURIParam: (url: PossibleURI, key: string) => URI;
96
+ /** Return a URI with no search params (or same URI if no changes were made). */
97
+ export declare function clearURIParams(url: URL | URLString, caller?: AnyCaller): URL;
98
+ export declare function clearURIParams(url: PossibleURI, caller?: AnyCaller): URI;
89
99
  /** A single schema for a URL. */
90
100
  export type URIScheme = `${string}:`;
91
101
  /** List of allowed URI schemes. */
package/util/uri.js CHANGED
@@ -43,6 +43,7 @@ export function requireURIString(possible, caller = requireURIString) {
43
43
  }
44
44
  /**
45
45
  * Get a set of entries for a set of possible URI params.
46
+ *- Any params with `undefined` value will be ignored.
46
47
  *
47
48
  * Note: Not as simple as just converting with `Object.fromEntries()`:
48
49
  * 1. When `URLSearchParams` contains multiple values for the same key, calling `params.get()` will return the _first_ value.
@@ -59,6 +60,8 @@ function* getURIEntries(input, caller = getURIParams) {
59
60
  else {
60
61
  const done = [];
61
62
  for (const [key, value] of getDictionaryItems(input)) {
63
+ if (value === undefined)
64
+ continue; // Skip undefined.
62
65
  if (done.includes(key))
63
66
  continue;
64
67
  done.push(key);
@@ -69,7 +72,10 @@ function* getURIEntries(input, caller = getURIParams) {
69
72
  }
70
73
  }
71
74
  }
72
- /** Get a set of params for a URI as a dictionary. */
75
+ /**
76
+ * Get a set of params for a URI as a dictionary.
77
+ * - Any params with `undefined` value will be ignored.
78
+ */
73
79
  export function getURIParams(input, caller = getURIParams) {
74
80
  const output = {};
75
81
  for (const [key, str] of getURIEntries(input, caller))
@@ -93,6 +99,8 @@ export function requireURIParam(input, key, caller = requireURIParam) {
93
99
  }
94
100
  export function withURIParam(url, key, value, caller = withURIParam) {
95
101
  const input = requireURI(url, caller);
102
+ if (value === undefined)
103
+ return input; // Ignore undefined.
96
104
  const output = new URI(input);
97
105
  const str = getString(value);
98
106
  if (str === undefined)
@@ -116,5 +124,13 @@ export function omitURIParams(url, ...keys) {
116
124
  }
117
125
  /** Return a URI without a param (or same URI if no changes were made). */
118
126
  export const omitURIParam = omitURIParams;
127
+ export function clearURIParams(url, caller = clearURIParams) {
128
+ const input = requireURI(url, caller);
129
+ if (!input.search.length)
130
+ return input;
131
+ const output = new URI(input);
132
+ output.search = "";
133
+ return output;
134
+ }
119
135
  /** Valid HTTP schemes for a URI. */
120
136
  export const HTTP_SCHEMES = ["http:", "https:"];