Added basic-tree

This commit is contained in:
2026-04-12 01:12:15 +02:00
parent c903e7bfa0
commit e3c7554ff3
6 changed files with 215 additions and 4 deletions

View File

@@ -26,3 +26,33 @@ export function required(description) {
export function typed_required(coercion_function, description) {
return new Field_Configuration((value) => value !== undefined, coercion_function, null, description);
}
export function symbol_set(description_to_name_mapping, description=null) {
const symbols_by_name = Object.fromEntries(Object.keys(description_to_name_mapping).map(k => [k, Symbol(k)]));
const valid_symbols = new Set(Object.values(symbols_by_name));
const descriptions_by_symbol = Object.fromEntries(Object.entries(symbols_by_name).map(([n, s]) => [s, description_to_name_mapping[n]]));
const result = new Field_Configuration(
(v) => valid_symbols.has(v),
(v) => typeof v === 'string' ? symbols_by_name[v] : v, // TODO: Assert that we could look up the symbol
null,
description,
);
// HACK: We are just tacking these on here but the proper method would be to create a proper subclass for the symbol set field type which is planned.
result.symbols = symbols_by_name;
result.symbol_descriptions = descriptions_by_symbol;
return result;
}
export function cardinal_value(default_value=null, description=null) {
return new Field_Configuration((v) => Number.isInteger(v) && v >= 1, parseInt, () => default_value, description);
}
export function natural_value(default_value=null, description=null) {
return new Field_Configuration((v) => Number.isInteger(v) && v >= 0, parseInt, () => default_value, description);
}