shelving
Version:
Toolkit for using data in JavaScript.
33 lines (32 loc) • 1.74 kB
TypeScript
import { type ImmutableArray } from "../util/array.js";
import type { SchemaOptions } from "./Schema.js";
import { Schema } from "./Schema.js";
export type ChoiceOptions<K extends string> = ImmutableArray<K> | {
readonly [KK in K]: unknown;
};
export type ChoiceOption<K extends string> = readonly [K, unknown];
/** Get the options for a choice field as an array. */
export declare function getChoiceEntries<K extends string>(options: ChoiceOptions<K>): ImmutableArray<ChoiceOption<K>>;
/** Get the keys for a choice field as an array. */
export declare function getChoiceKeys<K extends string>(options: ChoiceOptions<K>): ImmutableArray<K>;
/** Get the options for a choice field as an array. */
export declare function isOption<K extends string>(options: ChoiceOptions<K>, option: string): option is K;
/** Allowed options for `ChoiceSchema` */
export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
/** Specify correct options using a dictionary of entries. */
readonly options: ChoiceOptions<K>;
/** Default option for the value. */
readonly value?: K;
}
/** Choose from an allowed set of values. */
export declare class ChoiceSchema<K extends string> extends Schema<K> implements Iterable<ChoiceOption<K>> {
readonly value: K;
readonly options: ChoiceOptions<K>;
constructor({ one, title, placeholder, options, value, ...rest }: ChoiceSchemaOptions<K>);
validate(unsafeValue?: unknown): K;
[Symbol.iterator](): Iterator<ChoiceOption<K>>;
keys(): ImmutableArray<K>;
entries(): ImmutableArray<ChoiceOption<K>>;
}
/** Choose from an allowed set of values. */
export declare function CHOICE<K extends string>(options: ChoiceOptions<K>): ChoiceSchema<K>;