shelving 1.155.0 → 1.157.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 +1 -1
- package/schema/ChoiceSchema.d.ts +17 -4
- package/schema/ChoiceSchema.js +32 -5
package/package.json
CHANGED
package/schema/ChoiceSchema.d.ts
CHANGED
|
@@ -1,19 +1,32 @@
|
|
|
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. */
|
|
9
17
|
options: ChoiceOptions<K>;
|
|
18
|
+
/** Default option for the value. */
|
|
19
|
+
value?: K;
|
|
10
20
|
}
|
|
11
21
|
/** Choose from an allowed set of values. */
|
|
12
|
-
export declare class ChoiceSchema<K extends string> extends Schema<K> {
|
|
13
|
-
readonly value:
|
|
22
|
+
export declare class ChoiceSchema<K extends string> extends Schema<K> implements Iterable<ChoiceOption<K>> {
|
|
23
|
+
readonly value: K;
|
|
14
24
|
readonly options: ChoiceOptions<K>;
|
|
15
|
-
constructor({ options, ...rest }: ChoiceSchemaOptions<K>);
|
|
25
|
+
constructor({ options, value, ...rest }: ChoiceSchemaOptions<K>);
|
|
16
26
|
validate(unsafeValue?: unknown): K;
|
|
27
|
+
[Symbol.iterator](): Iterator<ChoiceOption<K>>;
|
|
28
|
+
keys(): ImmutableArray<K>;
|
|
29
|
+
entries(): ImmutableArray<ChoiceOption<K>>;
|
|
17
30
|
}
|
|
18
31
|
/** Choose from an allowed set of values. */
|
|
19
32
|
export declare function CHOICE<K extends string>(options: ChoiceOptions<K>): ChoiceSchema<K>;
|
package/schema/ChoiceSchema.js
CHANGED
|
@@ -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, ...rest }) {
|
|
9
|
-
super({ value
|
|
23
|
+
constructor({ options, value = requireFirst(isArray(options) ? options : getKeys(options)), ...rest }) {
|
|
24
|
+
super({ value, ...rest });
|
|
10
25
|
this.options = options;
|
|
11
26
|
}
|
|
12
27
|
validate(unsafeValue = this.value) {
|
|
13
|
-
if (typeof unsafeValue === "string" &&
|
|
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) {
|