shelving 1.187.0 → 1.187.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.187.0",
3
+ "version": "1.187.1",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,33 +3,34 @@ import type { SchemaOptions } from "./Schema.js";
3
3
  import { Schema } from "./Schema.js";
4
4
  /**
5
5
  * Set of options for a `ChoiceSchema` can be either:
6
- * - Array of string options in `[key]` format (`key` will be used as the `title` too).
7
6
  * - Dictionary of string options in `{ key: title }` format.
8
7
  */
9
- export type ChoiceOptions<K extends string> = ImmutableArray<K> | {
8
+ export type ChoiceOptions<K extends string> = {
10
9
  readonly [KK in K]: string;
11
10
  };
11
+ /**
12
+ * Things that can be converted to a choice options dictionary.
13
+ * - Array of string options in `[key]` format (`key` will be used as the `title` too).
14
+ */
15
+ export type PossibleChoiceOptions<K extends string> = ImmutableArray<K> | ChoiceOptions<K>;
12
16
  /** A single tuple for a choice option in `[key, title]` format. */
13
17
  export type ChoiceOption<K extends string> = readonly [title: K, title: string];
14
- /** Is an unknown string a choice option */
15
- export declare function isChoiceOption<K extends string>(options: ChoiceOptions<K>, option: string): option is K;
18
+ /** Get a `ChoiceOptions` object for a set of `PossibleChoiceOptions`. */
19
+ export declare function getChoiceOptions<K extends string>(options: PossibleChoiceOptions<K>): ChoiceOptions<K>;
16
20
  /** Allowed options for `ChoiceSchema` */
17
21
  export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
18
22
  /** Specify correct options using a dictionary of entries. */
19
- readonly options: ChoiceOptions<K>;
23
+ readonly options: PossibleChoiceOptions<K>;
20
24
  /** Default option for the value. */
21
25
  readonly value?: K;
22
26
  }
23
27
  /** Choose from an allowed set of values. */
24
- export declare class ChoiceSchema<K extends string> extends Schema<K> implements Iterable<ChoiceOption<K>> {
28
+ export declare class ChoiceSchema<K extends string> extends Schema<K> {
25
29
  readonly value: K | undefined;
26
30
  readonly options: ChoiceOptions<K>;
27
31
  constructor({ one, title, placeholder, options, value, ...rest }: ChoiceSchemaOptions<K>);
28
32
  validate(unsafeValue?: unknown): K;
29
33
  format(value: K): string;
30
- keys(): ImmutableArray<K>;
31
- entries(): ImmutableArray<ChoiceOption<K>>;
32
- [Symbol.iterator](): Iterator<ChoiceOption<K>>;
33
34
  }
34
35
  /** Choose from an allowed set of values. */
35
- export declare function CHOICE<K extends string>(options: ChoiceOptions<K> | ImmutableArray<K>): ChoiceSchema<K>;
36
+ export declare function CHOICE<K extends string>(options: PossibleChoiceOptions<K> | ImmutableArray<K>): ChoiceSchema<K>;
@@ -1,46 +1,27 @@
1
- import { isArray, isArrayItem } from "../util/array.js";
2
- import { getKeys, getProps, isProp } from "../util/object.js";
1
+ import { isArray } from "../util/array.js";
2
+ import { isProp } from "../util/object.js";
3
3
  import { Schema } from "./Schema.js";
4
- /** Is an unknown string a choice option */
5
- export function isChoiceOption(options, option) {
6
- return isArray(options) ? isArrayItem(options, option) : isProp(options, option);
4
+ /** Get a `ChoiceOptions` object for a set of `PossibleChoiceOptions`. */
5
+ export function getChoiceOptions(options) {
6
+ return isArray(options) ? Object.fromEntries(options.map(_getChoiceOption)) : options;
7
7
  }
8
- /** Get a dictionary from a plain array of options. */
9
- function* _yieldArrayChoiceOptions(options) {
10
- for (const k of options)
11
- yield [k, k];
8
+ function _getChoiceOption(k) {
9
+ return [k, k];
12
10
  }
13
11
  /** Choose from an allowed set of values. */
14
12
  export class ChoiceSchema extends Schema {
15
13
  options;
16
14
  constructor({ one = "choice", title = "Choice", placeholder = `No ${one}`, options, value, ...rest }) {
17
15
  super({ one, title, value, placeholder, ...rest });
18
- this.options = options;
16
+ this.options = getChoiceOptions(options);
19
17
  }
20
18
  validate(unsafeValue = this.value) {
21
- if (typeof unsafeValue === "string" && isChoiceOption(this.options, unsafeValue))
19
+ if (typeof unsafeValue === "string" && isProp(this.options, unsafeValue))
22
20
  return unsafeValue;
23
21
  throw unsafeValue ? `Unknown ${this.one}` : "Required";
24
22
  }
25
23
  format(value) {
26
- return isArray(this.options) ? value : this.options[value];
27
- }
28
- // Get the current list of keys for this choice.
29
- keys() {
30
- return isArray(this.options) ? this.options : getKeys(this.options);
31
- }
32
- // Get the current list of entries for this choice.
33
- entries() {
34
- return isArray(this.options) ? Array.from(_yieldArrayChoiceOptions(this.options)) : getProps(this.options);
35
- }
36
- // Implement iterable.
37
- *[Symbol.iterator]() {
38
- if (isArray(this.options)) {
39
- yield* _yieldArrayChoiceOptions(this.options);
40
- }
41
- else {
42
- yield* getProps(this.options);
43
- }
24
+ return this.options[value];
44
25
  }
45
26
  }
46
27
  /** Choose from an allowed set of values. */