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);
|
||||
}
|
||||
16
source/text/basic-tree.mjs
Normal file
16
source/text/basic-tree.mjs
Normal file
@@ -0,0 +1,16 @@
|
||||
import * as CF from '@efforting.tech/data/field-configuration-factories';
|
||||
|
||||
|
||||
export const Indention_Mode = new CF.symbol_set({
|
||||
AUTO: 'Automatic detection of indention mode',
|
||||
SPACES: 'Indention is based on spaces',
|
||||
TABULATORS: 'Indention is based on tabulators',
|
||||
}, 'Indention mode');
|
||||
|
||||
|
||||
// BUG: Current implementation of CF.symbol_set doesn't support default value
|
||||
export const Text_Settings = new CF.Schema({
|
||||
indention_mode: Indention_Mode,
|
||||
indention_tabulator_width: CF.cardinal_value(4, 'Width of a tabulator in spaces'),
|
||||
first_line: CF.natural_value(1, 'First line number'),
|
||||
}, 'Text settings');
|
||||
Reference in New Issue
Block a user