shelving 1.113.0 → 1.115.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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.113.0",
14
+ "version": "1.115.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -0,0 +1,14 @@
1
+ import type { AbsolutePath } from "../util/path.js";
2
+ import type { PossibleStarter } from "../util/start.js";
3
+ import { Store } from "./Store.js";
4
+ /** Store an absolute path, e.g. `/a/b/c` */
5
+ export declare class PathStore extends Store<AbsolutePath> {
6
+ constructor(path?: AbsolutePath, time?: number, start?: PossibleStarter<[Store<AbsolutePath>]>);
7
+ /** Based on the current store path, is a path active? */
8
+ isActive(path: AbsolutePath): boolean;
9
+ /** Based on the current store path, is a path proud (i.e. a child of the current store path)? */
10
+ isProud(path: AbsolutePath): boolean;
11
+ /** Get an absolute path from a path relative to the current navigation path. */
12
+ getAbsolute(path: string): AbsolutePath;
13
+ set(path: string): void;
14
+ }
@@ -0,0 +1,24 @@
1
+ import { getPath, isPathActive, isPathProud } from "../util/path.js";
2
+ import { Store } from "./Store.js";
3
+ /** Store an absolute path, e.g. `/a/b/c` */
4
+ export class PathStore extends Store {
5
+ constructor(path = window.location.pathname, time, start) {
6
+ super(path, time, start);
7
+ }
8
+ /** Based on the current store path, is a path active? */
9
+ isActive(path) {
10
+ return isPathActive(path, this.value);
11
+ }
12
+ /** Based on the current store path, is a path proud (i.e. a child of the current store path)? */
13
+ isProud(path) {
14
+ return isPathProud(path, this.value);
15
+ }
16
+ /** Get an absolute path from a path relative to the current navigation path. */
17
+ getAbsolute(path) {
18
+ return getPath(path, this.value);
19
+ }
20
+ // Override to clean the path.
21
+ set(path) {
22
+ super.set(getPath(path, this.value));
23
+ }
24
+ }
package/store/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from "./Store.js";
2
2
  export * from "./ArrayStore.js";
3
3
  export * from "./BooleanStore.js";
4
4
  export * from "./DataStore.js";
5
+ export * from "./PathStore.js";
5
6
  export * from "./DictionaryStore.js";
package/store/index.js CHANGED
@@ -2,4 +2,5 @@ export * from "./Store.js";
2
2
  export * from "./ArrayStore.js";
3
3
  export * from "./BooleanStore.js";
4
4
  export * from "./DataStore.js";
5
+ export * from "./PathStore.js";
5
6
  export * from "./DictionaryStore.js";
package/util/index.d.ts CHANGED
@@ -18,7 +18,6 @@ export * from "./equal.js";
18
18
  export * from "./error.js";
19
19
  export * from "./focus.js";
20
20
  export * from "./function.js";
21
- export * from "./start.js";
22
21
  export * from "./hash.js";
23
22
  export * from "./hydrate.js";
24
23
  export * from "./item.js";
@@ -32,6 +31,7 @@ export * from "./null.js";
32
31
  export * from "./number.js";
33
32
  export * from "./object.js";
34
33
  export * from "./optional.js";
34
+ export * from "./path.js";
35
35
  export * from "./query.js";
36
36
  export * from "./random.js";
37
37
  export * from "./regexp.js";
@@ -39,6 +39,7 @@ export * from "./sequence.js";
39
39
  export * from "./serialise.js";
40
40
  export * from "./sort.js";
41
41
  export * from "./source.js";
42
+ export * from "./start.js";
42
43
  export * from "./string.js";
43
44
  export * from "./template.js";
44
45
  export * from "./time.js";
package/util/index.js CHANGED
@@ -18,7 +18,6 @@ export * from "./equal.js";
18
18
  export * from "./error.js";
19
19
  export * from "./focus.js";
20
20
  export * from "./function.js";
21
- export * from "./start.js";
22
21
  export * from "./hash.js";
23
22
  export * from "./hydrate.js";
24
23
  export * from "./item.js";
@@ -32,6 +31,7 @@ export * from "./null.js";
32
31
  export * from "./number.js";
33
32
  export * from "./object.js";
34
33
  export * from "./optional.js";
34
+ export * from "./path.js";
35
35
  export * from "./query.js";
36
36
  export * from "./random.js";
37
37
  export * from "./regexp.js";
@@ -39,6 +39,7 @@ export * from "./sequence.js";
39
39
  export * from "./serialise.js";
40
40
  export * from "./sort.js";
41
41
  export * from "./source.js";
42
+ export * from "./start.js";
42
43
  export * from "./string.js";
43
44
  export * from "./template.js";
44
45
  export * from "./time.js";
package/util/path.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /** Absolute path starts with `/` slash. */
2
+ export type AbsolutePath = `/` | `/${string}`;
3
+ /** Relative path starts with `./` or `../` */
4
+ export type RelativePath = `.` | `./${string}` | `..` | `../${string}`;
5
+ /** Is a string path an absolute path? */
6
+ export declare const isAbsolutePath: (path: string) => path is `/${string}`;
7
+ /** Is a string path an absolute path? */
8
+ export declare const isRelativePath: (path: string) => path is RelativePath;
9
+ /**
10
+ * Clean a path.
11
+ * - Runs of `//` two or more slashes are normalised to `/` single slash, e.g. `/a//b` becomes `/a/b`
12
+ * - `\` Windows slashes are nromalised to `/` UNIX slashes.
13
+ * - Trailing slashes are removed, e.g. `/a/b/` becomes `/a/b`
14
+ */
15
+ export declare function cleanPath(path: AbsolutePath): AbsolutePath;
16
+ export declare function cleanPath(path: string): string;
17
+ /**
18
+ * Resolve an absolute path.
19
+ * - Uses `new URL` to do path processing, so URL strings e.g.
20
+ * - Returned paths are cleaned with `cleanPath()` so runs of slashes and trailing slashes are removed.
21
+ *
22
+ * @param path Absolute path e.g. `/a/b/c`, relative path e.g. `./a` or `b` or `../c`, URL string e.g. `http://shax.com/a/b/c`, or `URL` instance.
23
+ * @param base Absolute path or `URL` instance used for resolving relative paths in `path`
24
+ * @return Absolute path with a leading trailing slash, e.g. `/a/c/b`
25
+ */
26
+ export declare function getPath(path: string | URL, base?: AbsolutePath | URL): AbsolutePath;
27
+ /** Is a target path active? */
28
+ export declare function isPathActive(target: AbsolutePath, current: AbsolutePath): boolean;
29
+ /** Is a target path proud (i.e. is the current path, or is a child of the current path)? */
30
+ export declare function isPathProud(target: AbsolutePath, current: AbsolutePath): boolean;
package/util/path.js ADDED
@@ -0,0 +1,35 @@
1
+ import { AssertionError } from "../error/AssertionError.js";
2
+ /** Is a string path an absolute path? */
3
+ export const isAbsolutePath = (path) => path.startsWith("/");
4
+ /** Is a string path an absolute path? */
5
+ export const isRelativePath = (path) => path.startsWith("./") || path.startsWith("../");
6
+ export function cleanPath(path) {
7
+ return path
8
+ .replace(/[/\\]+/g, "/") // Normalise slashes.
9
+ .replace(/(?!^)\/$/g, ""); // Trailing slashes.
10
+ }
11
+ /**
12
+ * Resolve an absolute path.
13
+ * - Uses `new URL` to do path processing, so URL strings e.g.
14
+ * - Returned paths are cleaned with `cleanPath()` so runs of slashes and trailing slashes are removed.
15
+ *
16
+ * @param path Absolute path e.g. `/a/b/c`, relative path e.g. `./a` or `b` or `../c`, URL string e.g. `http://shax.com/a/b/c`, or `URL` instance.
17
+ * @param base Absolute path or `URL` instance used for resolving relative paths in `path`
18
+ * @return Absolute path with a leading trailing slash, e.g. `/a/c/b`
19
+ */
20
+ export function getPath(path, base = "/") {
21
+ try {
22
+ return cleanPath(new URL(path, `http://j.com${base instanceof URL ? base.pathname : base}/`).pathname);
23
+ }
24
+ catch {
25
+ throw new AssertionError("Invalid path", path);
26
+ }
27
+ }
28
+ /** Is a target path active? */
29
+ export function isPathActive(target, current) {
30
+ return target === current;
31
+ }
32
+ /** Is a target path proud (i.e. is the current path, or is a child of the current path)? */
33
+ export function isPathProud(target, current) {
34
+ return target === current || (target !== "/" && target.startsWith(`${current}/`));
35
+ }
package/util/string.d.ts CHANGED
@@ -62,7 +62,7 @@ export declare const sanitizeLines: (str: string) => string;
62
62
  * Simplify a string by removing anything that isn't a number, letter, or space.
63
63
  * - Used when you're running a query against a string entered by a user.
64
64
  *
65
- * @example normalizeString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
65
+ * @example simplifyString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
66
66
  *
67
67
  * @todo Convert letter-like characters (e.g. `ℝ`) to their ASCII equivalent (e.g. `R`).
68
68
  */
package/util/string.js CHANGED
@@ -98,7 +98,7 @@ export const sanitizeLines = (str) => str
98
98
  * Simplify a string by removing anything that isn't a number, letter, or space.
99
99
  * - Used when you're running a query against a string entered by a user.
100
100
  *
101
- * @example normalizeString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
101
+ * @example simplifyString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
102
102
  *
103
103
  * @todo Convert letter-like characters (e.g. `ℝ`) to their ASCII equivalent (e.g. `R`).
104
104
  */