shelving 1.156.0 → 1.157.1

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/index.d.ts CHANGED
@@ -7,7 +7,6 @@ export * from "./api/index.js";
7
7
  export * from "./db/index.js";
8
8
  export * from "./error/index.js";
9
9
  export * from "./feedback/index.js";
10
- export * from "./iterate/index.js";
11
10
  export * from "./markup/index.js";
12
11
  export * from "./schema/index.js";
13
12
  export * from "./sequence/index.js";
package/index.js CHANGED
@@ -10,7 +10,6 @@ export * from "./feedback/index.js";
10
10
  // export * from "./firestore/client/index.js"; // Not exported.
11
11
  // export * from "./firestore/lite/index.js"; // Not exported.
12
12
  // export * from "./firestore/server/index.js"; // Not exported.
13
- export * from "./iterate/index.js";
14
13
  export * from "./markup/index.js";
15
14
  // export * from "./react/index.js"; // Not exported.
16
15
  export * from "./schema/index.js";
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.156.0",
14
+ "version": "1.157.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -1,8 +1,16 @@
1
+ import { type ImmutableArray } from "../util/array.js";
1
2
  import type { SchemaOptions } from "./Schema.js";
2
3
  import { Schema } from "./Schema.js";
3
- export type ChoiceOptions<K extends string> = {
4
+ export type ChoiceOptions<K extends string> = ImmutableArray<K> | {
4
5
  readonly [KK in K]: unknown;
5
6
  };
7
+ export type ChoiceOption<K extends string> = readonly [K, unknown];
8
+ /** Get the options for a choice field as an array. */
9
+ export declare function getChoiceEntries<K extends string>(options: ChoiceOptions<K>): ImmutableArray<ChoiceOption<K>>;
10
+ /** Get the keys for a choice field as an array. */
11
+ export declare function getChoiceKeys<K extends string>(options: ChoiceOptions<K>): ImmutableArray<K>;
12
+ /** Get the options for a choice field as an array. */
13
+ export declare function isOption<K extends string>(options: ChoiceOptions<K>, option: string): option is K;
6
14
  /** Allowed options for `ChoiceSchema` */
7
15
  export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
8
16
  /** Specify correct options using a dictionary of entries. */
@@ -11,11 +19,14 @@ export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOption
11
19
  value?: K;
12
20
  }
13
21
  /** Choose from an allowed set of values. */
14
- export declare class ChoiceSchema<K extends string> extends Schema<K> {
15
- readonly value: string;
22
+ export declare class ChoiceSchema<K extends string> extends Schema<K> implements Iterable<ChoiceOption<K>> {
23
+ readonly value: K;
16
24
  readonly options: ChoiceOptions<K>;
17
25
  constructor({ options, value, ...rest }: ChoiceSchemaOptions<K>);
18
26
  validate(unsafeValue?: unknown): K;
27
+ [Symbol.iterator](): Iterator<ChoiceOption<K>>;
28
+ keys(): ImmutableArray<K>;
29
+ entries(): ImmutableArray<ChoiceOption<K>>;
19
30
  }
20
31
  /** Choose from an allowed set of values. */
21
32
  export declare function CHOICE<K extends string>(options: ChoiceOptions<K>): ChoiceSchema<K>;
@@ -1,19 +1,46 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { requireFirst } from "../util/array.js";
3
- import { getKeys, isProp } from "../util/object.js";
2
+ import { isArray, requireFirst } from "../util/array.js";
3
+ import { getKeys, getProps, isProp } from "../util/object.js";
4
4
  import { Schema } from "./Schema.js";
5
+ /** Get the options for a choice field as an array. */
6
+ export function getChoiceEntries(options) {
7
+ return isArray(options) ? options.map(_makeKeyEntry) : getProps(options);
8
+ }
9
+ function _makeKeyEntry(k) {
10
+ return [k, k];
11
+ }
12
+ /** Get the keys for a choice field as an array. */
13
+ export function getChoiceKeys(options) {
14
+ return isArray(options) ? options : getKeys(options);
15
+ }
16
+ /** Get the options for a choice field as an array. */
17
+ export function isOption(options, option) {
18
+ return isArray(options) ? options.includes(option) : isProp(options, option);
19
+ }
5
20
  /** Choose from an allowed set of values. */
6
21
  export class ChoiceSchema extends Schema {
7
22
  options;
8
- constructor({ options, value = requireFirst(getKeys(options)), ...rest }) {
23
+ constructor({ options, value = requireFirst(isArray(options) ? options : getKeys(options)), ...rest }) {
9
24
  super({ value, ...rest });
10
25
  this.options = options;
11
26
  }
12
27
  validate(unsafeValue = this.value) {
13
- if (typeof unsafeValue === "string" && isProp(this.options, unsafeValue))
28
+ if (typeof unsafeValue === "string" && isOption(this.options, unsafeValue))
14
29
  return unsafeValue;
15
30
  throw new ValueFeedback("Unknown value", unsafeValue);
16
31
  }
32
+ // Implement iterable.
33
+ *[Symbol.iterator]() {
34
+ yield* getChoiceEntries(this.options);
35
+ }
36
+ // Get the current list of keys for this choice.
37
+ keys() {
38
+ return getChoiceKeys(this.options);
39
+ }
40
+ // Get the current list of entries for this choice.
41
+ entries() {
42
+ return getChoiceEntries(this.options);
43
+ }
17
44
  }
18
45
  /** Choose from an allowed set of values. */
19
46
  export function CHOICE(options) {
@@ -43,10 +43,7 @@ export declare class StringSchema extends Schema<string> {
43
43
  /** Valid string, e.g. `Hello there!` */
44
44
  export declare const STRING: StringSchema;
45
45
  /** Valid string, `Hello there!`, with more than one character. */
46
- export declare const REQUIRED_STRING: StringSchema; /** Valid text, e.g. `Hello there!` */
47
- export declare const TEXT: StringSchema;
48
- /** Valid text, `Hello there!`, with more than one character. */
49
- export declare const REQUIRED_TEXT: StringSchema;
46
+ export declare const REQUIRED_STRING: StringSchema;
50
47
  /** Title string, e.g. `Title of something` */
51
48
  export declare const TITLE: StringSchema;
52
49
  /** Optional name string, e.g. `Title of something` or `null` */
@@ -59,10 +59,7 @@ export class StringSchema extends Schema {
59
59
  /** Valid string, e.g. `Hello there!` */
60
60
  export const STRING = new StringSchema({});
61
61
  /** Valid string, `Hello there!`, with more than one character. */
62
- export const REQUIRED_STRING = new StringSchema({ min: 1 }); /** Valid text, e.g. `Hello there!` */
63
- export const TEXT = new StringSchema({ title: "Text" });
64
- /** Valid text, `Hello there!`, with more than one character. */
65
- export const REQUIRED_TEXT = new StringSchema({ min: 1 });
62
+ export const REQUIRED_STRING = new StringSchema({ min: 1 });
66
63
  /** Title string, e.g. `Title of something` */
67
64
  export const TITLE = new StringSchema({ title: "Title", min: 1, max: 100 });
68
65
  /** Optional name string, e.g. `Title of something` or `null` */
@@ -1,7 +0,0 @@
1
- /** Abstract generator designed to be extended that implements the full generator protocol. */
2
- export declare abstract class AbstractIterator<T, R, N> implements Iterator<T, R, N>, Iterable<T, R, N> {
3
- abstract next(value: N): IteratorResult<T, R>;
4
- throw(thrown: unknown): IteratorResult<T, R>;
5
- return(value: R): IteratorResult<T, R>;
6
- [Symbol.iterator](): Iterator<T, R, N>;
7
- }
@@ -1,15 +0,0 @@
1
- /** Abstract generator designed to be extended that implements the full generator protocol. */
2
- export class AbstractIterator {
3
- throw(thrown) {
4
- // Default behaviour for a generator is to throw the error back out of the iterator and not continue.
5
- throw thrown;
6
- }
7
- return(value) {
8
- // Default behaviour for a generator is to return `done: true` and the input value.
9
- return { done: true, value };
10
- }
11
- // Implement `Iterable`
12
- [Symbol.iterator]() {
13
- return this;
14
- }
15
- }
@@ -1,31 +0,0 @@
1
- import { ThroughIterator } from "./ThroughIterator.js";
2
- /**
3
- * Iterable that inspects a source iterable as it iterates.
4
- * - Stores: first/last yielded value, returned value, whether iteration is done, the number of items that were iterated.
5
- *
6
- * @example
7
- * const watch = new InspectIterator(iterable);
8
- * for (const next of capture) console.log("YIELDED", next);
9
- * console.log("FIRST", watch.first);
10
- * console.log("RETURNED", watch.returned);
11
- */
12
- export declare class InspectIterator<T, R, N> extends ThroughIterator<T, R, N> implements Iterator<T, R, N>, Iterable<T, R, N> {
13
- /** Get the number of results received by this iterator so far. */
14
- readonly count = 0;
15
- /** Is the iteration done? */
16
- readonly done: boolean;
17
- /** The first yielded value (throws if the iteration yielded no values, i.e. `this.count === 0`). */
18
- get first(): T;
19
- private _first;
20
- /** The last yielded value (throws if the iteration yielded no values, i.e. `this.count === 0`). */
21
- get last(): T;
22
- private _last;
23
- /** The returned value (throws if the iteration is not done, i.e. `this.done === false`). */
24
- get returned(): R;
25
- private _returned;
26
- next(value: N): IteratorResult<T, R>;
27
- throw(thrown: unknown): IteratorResult<T, R>;
28
- return(value: R): IteratorResult<T, R>;
29
- /** Capture a result. */
30
- private _inspect;
31
- }
@@ -1,75 +0,0 @@
1
- import { UnexpectedError } from "../error/UnexpectedError.js";
2
- import { getGetter } from "../util/class.js";
3
- import { ThroughIterator } from "./ThroughIterator.js";
4
- /** Used when the sequence hasn't inspected anything yet. */
5
- const _NOVALUE = Symbol("shelving/InspectGenerator.NOVALUE");
6
- /**
7
- * Iterable that inspects a source iterable as it iterates.
8
- * - Stores: first/last yielded value, returned value, whether iteration is done, the number of items that were iterated.
9
- *
10
- * @example
11
- * const watch = new InspectIterator(iterable);
12
- * for (const next of capture) console.log("YIELDED", next);
13
- * console.log("FIRST", watch.first);
14
- * console.log("RETURNED", watch.returned);
15
- */
16
- export class InspectIterator extends ThroughIterator {
17
- /** Get the number of results received by this iterator so far. */
18
- count = 0;
19
- /** Is the iteration done? */
20
- done = false;
21
- /** The first yielded value (throws if the iteration yielded no values, i.e. `this.count === 0`). */
22
- get first() {
23
- if (this._first === _NOVALUE)
24
- throw new UnexpectedError("Iteration not started", {
25
- iterator: this,
26
- caller: getGetter(this, "first"),
27
- });
28
- return this._first;
29
- }
30
- _first = _NOVALUE;
31
- /** The last yielded value (throws if the iteration yielded no values, i.e. `this.count === 0`). */
32
- get last() {
33
- if (this._last === _NOVALUE)
34
- throw new UnexpectedError("Iteration not started", {
35
- iterator: this,
36
- caller: getGetter(this, "last"),
37
- });
38
- return this._last;
39
- }
40
- _last = _NOVALUE;
41
- /** The returned value (throws if the iteration is not done, i.e. `this.done === false`). */
42
- get returned() {
43
- if (this._returned === _NOVALUE)
44
- throw new UnexpectedError("Iteration not done", {
45
- iterator: this,
46
- caller: getGetter(this, "returned"),
47
- });
48
- return this._returned;
49
- }
50
- _returned = _NOVALUE;
51
- // Override to watch returned values.
52
- next(value) {
53
- return this._inspect(this.next(value));
54
- }
55
- throw(thrown) {
56
- return this._inspect(this.throw(thrown));
57
- }
58
- return(value) {
59
- return this._inspect(this.return(value));
60
- }
61
- /** Capture a result. */
62
- _inspect(result) {
63
- if (!result.done) {
64
- if (this.first === undefined)
65
- this._first = result.value;
66
- this._last = result.value;
67
- this.count++;
68
- }
69
- else {
70
- this._returned = result.value;
71
- this.done = true;
72
- }
73
- return result;
74
- }
75
- }
@@ -1,9 +0,0 @@
1
- import { AbstractIterator } from "./AbstractIterator.js";
2
- /** Iterable that pulls values from a source iterable. */
3
- export declare class ThroughIterator<T, R, N> extends AbstractIterator<T, R, N> implements Iterator<T, R, N>, Iterable<T, R, N> {
4
- private readonly _source;
5
- constructor(iterator: Iterator<T, R, N>);
6
- next(value: N): IteratorResult<T, R>;
7
- throw(thrown: unknown): IteratorResult<T, R>;
8
- return(value: R): IteratorResult<T, R>;
9
- }
@@ -1,19 +0,0 @@
1
- import { AbstractIterator } from "./AbstractIterator.js";
2
- /** Iterable that pulls values from a source iterable. */
3
- export class ThroughIterator extends AbstractIterator {
4
- _source;
5
- constructor(iterator) {
6
- super();
7
- this._source = iterator;
8
- }
9
- // Implement `AbstractIterator`
10
- next(value) {
11
- return this._source.next(value);
12
- }
13
- throw(thrown) {
14
- return this._source.throw ? this._source.throw(thrown) : super.throw(thrown);
15
- }
16
- return(value) {
17
- return this._source.return ? this._source.return(value) : super.return(value);
18
- }
19
- }
@@ -1,3 +0,0 @@
1
- export * from "./AbstractIterator.js";
2
- export * from "./ThroughIterator.js";
3
- export * from "./InspectIterator.js";
package/iterate/index.js DELETED
@@ -1,3 +0,0 @@
1
- export * from "./AbstractIterator.js";
2
- export * from "./ThroughIterator.js";
3
- export * from "./InspectIterator.js";