enumValue
functionBuild a SerializationSchema for a fixed set of allowed `values` (an enumeration or a union of literals such as the `mode` of a TextNode). Returns `value` when it is strictly equal to one of `values`, otherwise returns `defaultValue`, which defaults to the first entry of `values`. The type parameter is `const`, so the literal types of `values` are inferred directly — the caller does not need an `as const` assertion. (Pass an explicit type argument, e.g. `enumValue<TextModeType>([...])`, to instead assert the values against a known domain type.) `undefined` may be a member of the domain, and a declared `undefined` default is taken as declared: `enumValue([undefined, 'middle', 'bottom'])` and `enumValue(['middle', undefined], undefined)` both default to `undefined`. `values` must be non-empty, which the type states as a tuple: an empty domain admits nothing, so every value — including one the caller believes is in the enum — would parse to a default that came from nowhere. A list built at runtime is checked as well in a development build, since a type can be asserted past.
Import
import { enumValue } from '@modular/sdk/editor';Notes
- example
- ```ts const parseMode = enumValue(['normal', 'token', 'segmented']); // ^? SerializationSchema<'normal' | 'token' | 'segmented'>, default 'normal' ```
- __NO_SIDE_EFFECTS__