Improved dispatchers with richer results and added predicate and regular expression based dispatchers
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Mapping_Resolver, Chained_Resolver } from '@efforting.tech/rule-processing/resolvers';
|
||||
|
||||
|
||||
import { inspect } from 'node:util';
|
||||
|
||||
|
||||
const vr = new Mapping_Resolver();
|
||||
@@ -9,9 +9,55 @@ const tr = new Mapping_Resolver(new Map(), item => typeof item);
|
||||
const cr = new Chained_Resolver([vr, tr]);
|
||||
|
||||
|
||||
vr.rules.set('HELLO', () => 'WORLD');
|
||||
tr.rules.set('string', () => 'World');
|
||||
vr.rules.set('HELLO', {handler: () => 'WORLD'});
|
||||
tr.rules.set('string', {extra_stuff: 'Yo!', handler: (c) => `World with context: ${inspect(c)}`});
|
||||
|
||||
console.log(cr.resolve('HELLO'));
|
||||
console.log(cr.resolve('hello'));
|
||||
console.log(cr.resolve(123));
|
||||
|
||||
|
||||
/* OUTPUT
|
||||
|
||||
WORLD
|
||||
World with context: {
|
||||
resolver: Chained_Resolver {
|
||||
chain_links: [ [Mapping_Resolver], [Mapping_Resolver] ]
|
||||
},
|
||||
item: 'hello',
|
||||
extra_stuff: 'Yo!',
|
||||
handler: [Function: handler]
|
||||
}
|
||||
file:///srv/Projekt/efforting.tech/nodejs.esm-library/build/packages/rule-processing/resolv
|
||||
throw new Item_Unresolvable({ resolver: this, item });
|
||||
^
|
||||
|
||||
Item_Unresolvable [Error]: Cannot resolve item 123 of type "number" using resolver Chained_
|
||||
at Chained_Resolver.resolve (file:///srv/Projekt/efforting.tech/nodejs.esm-library/buil
|
||||
at file:///srv/Projekt/efforting.tech/nodejs.esm-library/experiments/res1.mjs:17:16
|
||||
at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
|
||||
at async node:internal/modules/esm/loader:639:26
|
||||
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
|
||||
data: {
|
||||
resolver: Chained_Resolver {
|
||||
chain_links: [
|
||||
Mapping_Resolver {
|
||||
rules: Map(1) { 'HELLO' => [Object] },
|
||||
key_function: null
|
||||
},
|
||||
Mapping_Resolver {
|
||||
rules: Map(1) { 'string' => [Object] },
|
||||
key_function: [Function (anonymous)]
|
||||
}
|
||||
]
|
||||
},
|
||||
item: 123
|
||||
}
|
||||
}
|
||||
|
||||
Node.js v25.8.2
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
36
experiments/text-nodes-dispatch.mjs
Normal file
36
experiments/text-nodes-dispatch.mjs
Normal file
@@ -0,0 +1,36 @@
|
||||
import * as CF from '@efforting.tech/data/field-configuration-factories';
|
||||
|
||||
import { inspect } from 'node:util';
|
||||
import { Text_Tree_Node, Text_Tree_Settings } from '@efforting.tech/text/basic-tree';
|
||||
import { RegExp_Resolver } from '@efforting.tech/rule-processing/resolvers';
|
||||
|
||||
|
||||
|
||||
const ts = Text_Tree_Settings.load({
|
||||
text: {
|
||||
indention_mode: 'TABULATORS',
|
||||
},
|
||||
trim_lines: true,
|
||||
});
|
||||
|
||||
const example_string =
|
||||
`
|
||||
animals: dog, cat
|
||||
trees: birch, pine
|
||||
|
||||
`;
|
||||
|
||||
|
||||
const root = Text_Tree_Node.from_string(ts, example_string);
|
||||
|
||||
|
||||
const d = new RegExp_Resolver([
|
||||
[/^animals:\s*(.*)$/, (c) => console.log("ANIMAL", c)],
|
||||
[/^trees:\s*(.*)$/, () => console.log("TREE")],
|
||||
]);
|
||||
|
||||
for (const child of root.children) {
|
||||
if (child.has_line) {
|
||||
console.log(child.line, d.resolve(child.line));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import * as CF from '@efforting.tech/data/field-configuration-factories';
|
||||
|
||||
import { inspect } from 'node:util';
|
||||
|
||||
import { Indention_Mode, Text_Settings, string_has_contents, indented_line_iterator } from '@efforting.tech/data/string-utilities';
|
||||
import { Text_Node } from '@efforting.tech/text/basic-tree';
|
||||
import { Text_Tree_Node, Text_Tree_Settings } from '@efforting.tech/text/basic-tree';
|
||||
|
||||
|
||||
const ts = Text_Settings.load({
|
||||
indention_mode: 'TABULATORS',
|
||||
const ts = Text_Tree_Settings.load({
|
||||
text: {
|
||||
indention_mode: 'TABULATORS',
|
||||
},
|
||||
});
|
||||
|
||||
const example_string =
|
||||
@@ -30,7 +30,7 @@ branch3
|
||||
`;
|
||||
|
||||
|
||||
const root = Text_Node.from_string(ts, example_string);
|
||||
const root = Text_Tree_Node.from_string(ts, example_string);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user