Added basic-tree
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user