Skip to main content

aliasedValue

function

Combinator for a value that older documents may spell as one of a fixed set of names — TextNode's `format: 'bold'` for the numeric bit it stands for. A string matching one of `aliases` yields the value it names; anything else is `inner`'s to validate, so the domain, the default and the equality all stay `inner`'s and only the accepted *input* is wider. This is transformValue narrowed to the case where the normalization is a lookup, and the reason to prefer it is that the lookup is data: it goes into the schema's meta, where a tool can see it. A `transformValue` keeps its function to itself, so its meta can say only that a transform happens: example generation still reaches the inner domain, and a code generator refuses the property rather than compile a parse that stores the alias where the schema stores what it names.

function aliasedValue<T, const A extends { readonly [alias: string]: T; }, In = T>(inner: SerializationSchema<T, never, In>, aliases: A): SerializationSchema<T, never, In | Extract<keyof A, string>>

Import

import { aliasedValue } from '@modular/sdk/editor';

Notes

example
```ts const parseFormat = aliasedValue(numberValue(), TEXT_TYPE_TO_FORMAT); // ^? SerializationSchema<number> parseFormat(1); // 1 parseFormat('bold'); // IS_BOLD parseFormat('42'); // 42 (not an alias, so numberValue reads it) parseFormat('junk'); // 0 (numberValue falls back to its default) ```
__NO_SIDE_EFFECTS__