Compare commits
13 Commits
v0.2.4
...
cf1abadfc9
| Author | SHA1 | Date | |
|---|---|---|---|
| cf1abadfc9 | |||
| 225990b22d | |||
| 81035737a2 | |||
| a02e0f5800 | |||
| c604957e2c | |||
| e3c7554ff3 | |||
| c903e7bfa0 | |||
| 8442383fc3 | |||
| 25dc8b8d0f | |||
| ae40c680de | |||
| 0cdc4e271b | |||
| 31b6eecdec | |||
| a088f97e2e |
2
Makefile
2
Makefile
@@ -2,7 +2,7 @@
|
||||
|
||||
build/packages:
|
||||
mkdir -p $@
|
||||
node tools/stage-for-pnpn.mjs package-manifest.yaml source $@
|
||||
node tools/stage-for-pnpm.mjs package-manifest.yaml source $@
|
||||
|
||||
publish:
|
||||
cd build/packages && ./publish-all.sh
|
||||
|
||||
9
documentation/data/readme.md
Normal file
9
documentation/data/readme.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# @efforting.tech/data
|
||||
|
||||
Data processing modules.
|
||||
|
||||
**TODO:** *Explain in more detail what this vague description actually refers to.*
|
||||
|
||||
## field-configuration-factories.mjs
|
||||
|
||||
Currently there are a few factories defined but we might add more as specific needs arises throughout the library.
|
||||
50
experiments/config1.mjs
Normal file
50
experiments/config1.mjs
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Schema, Field_Configuration } from '@efforting.tech/data/field-configuration';
|
||||
|
||||
|
||||
function mandatory_anything(value) {
|
||||
return value !== undefined;
|
||||
}
|
||||
|
||||
|
||||
function string_coercion_function(value) {
|
||||
if (value === undefined) {
|
||||
throw new Error('Undefined not allowed');
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const fc = new Field_Configuration(null, string_coercion_function, null, 'Anything that could be a string');
|
||||
const fd = new Field_Configuration(null, string_coercion_function, () => 'baz', 'A string. Defaults to \'baz\'');
|
||||
|
||||
console.log(fc.check_validation(undefined));
|
||||
console.log(fc.check_validation(true));
|
||||
|
||||
console.log([ fc.coerce(123) ]);
|
||||
console.log([ fc.coerce(true) ]);
|
||||
|
||||
// console.log([ fc.load(undefined, 'Some configuration') ]);
|
||||
// console.log([ fc.load(true) ]);
|
||||
|
||||
|
||||
//fc.validate(undefined);
|
||||
//console.log(fc.load(undefined, 'New thingamabob object'));
|
||||
|
||||
|
||||
const s = new Schema({
|
||||
foo: fc,
|
||||
bar: fd,
|
||||
});
|
||||
|
||||
const s2 = new Schema({
|
||||
thing: fc,
|
||||
stuff: s,
|
||||
});
|
||||
|
||||
console.log(s2.load({
|
||||
stuff: { foo: 'Hello World' },
|
||||
thing: 123,
|
||||
}));
|
||||
|
||||
// { thing: '123', stuff: { foo: 'Hello World', bar: 'baz' } }
|
||||
10
experiments/config2.mjs
Normal file
10
experiments/config2.mjs
Normal file
@@ -0,0 +1,10 @@
|
||||
import * as F from '@efforting.tech/data/field-configuration-factories';
|
||||
|
||||
const s = new F.Schema({
|
||||
foo: F.value(123, 'The value'),
|
||||
bar: F.factory((t) => `Field ${t.name} was not set`, 'The factory'),
|
||||
rq: F.required('Anything. Just not nothing.'),
|
||||
}, 'Some schema');
|
||||
|
||||
|
||||
console.log(s.load()) // { foo: 123, bar: 'Field bar was not set' }
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"name": "experiments",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@efforting.tech/errors": "link:../build/packages/errors",
|
||||
"@efforting.tech/rule-processing": "link:../build/packages/rule-processing"
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
69
experiments/text-nodes.mjs
Normal file
69
experiments/text-nodes.mjs
Normal file
@@ -0,0 +1,69 @@
|
||||
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';
|
||||
|
||||
|
||||
const ts = Text_Tree_Settings.load({
|
||||
text: {
|
||||
indention_mode: 'TABULATORS',
|
||||
},
|
||||
});
|
||||
|
||||
const example_string =
|
||||
`branch1
|
||||
leaf1
|
||||
leaf2
|
||||
|
||||
branch2
|
||||
sub-branch1
|
||||
leaf3
|
||||
leaf4
|
||||
|
||||
|
||||
sub-branch2
|
||||
leaf5
|
||||
|
||||
branch3
|
||||
dual-indented
|
||||
|
||||
`;
|
||||
|
||||
|
||||
const root = Text_Tree_Node.from_string(ts, example_string);
|
||||
|
||||
|
||||
|
||||
function debug_dump(node, level=0) {
|
||||
console.log(`${" ".repeat(level)}[${node.line_no ?? '-'}] ${inspect(node.line)}`);
|
||||
for (const child of node.children) {
|
||||
debug_dump(child, level+1);
|
||||
}
|
||||
}
|
||||
|
||||
debug_dump(root);
|
||||
|
||||
|
||||
/*
|
||||
|
||||
[-] undefined
|
||||
[1] 'branch1'
|
||||
[2] 'leaf1'
|
||||
[3] 'leaf2'
|
||||
[4] ''
|
||||
[5] 'branch2'
|
||||
[6] 'sub-branch1'
|
||||
[7] 'leaf3'
|
||||
[8] 'leaf4'
|
||||
[9] ''
|
||||
[10] ''
|
||||
[11] 'sub-branch2'
|
||||
[12] 'leaf5'
|
||||
[13] ''
|
||||
[14] 'branch3'
|
||||
[-] undefined
|
||||
[15] 'dual-indented'
|
||||
[16] ''
|
||||
[17] ''
|
||||
|
||||
*/
|
||||
@@ -1,6 +1,6 @@
|
||||
scope: '@efforting.tech'
|
||||
registry: 'https://npm.efforting.tech/'
|
||||
version: 0.2.4
|
||||
version: 0.2.9
|
||||
|
||||
author:
|
||||
name: 'Mikael Lövqvist'
|
||||
@@ -21,6 +21,19 @@ packages:
|
||||
internal-dependencies:
|
||||
- errors
|
||||
|
||||
data:
|
||||
path: source/data
|
||||
documentation: documentation/data
|
||||
description: Data management
|
||||
internal-dependencies:
|
||||
- errors
|
||||
|
||||
text:
|
||||
path: source/text
|
||||
#documentation: documentation/text
|
||||
description: Text management
|
||||
internal-dependencies:
|
||||
- data
|
||||
|
||||
wip-packages:
|
||||
object-graph-storage:
|
||||
|
||||
64
source/data/field-configuration-factories.mjs
Normal file
64
source/data/field-configuration-factories.mjs
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Field_Configuration } from './field-configuration.mjs';
|
||||
export { Schema } from './field-configuration.mjs';
|
||||
|
||||
//constructor(validation_function=null, coercion_function=null, factory_function=null, expected_description=undefined) {
|
||||
|
||||
export function value(default_value, description) {
|
||||
return new Field_Configuration(null, null, () => default_value, description);
|
||||
}
|
||||
|
||||
export function factory(factory_function, description) {
|
||||
return new Field_Configuration(null, null, factory_function, description);
|
||||
}
|
||||
|
||||
export function typed_value(coercion_function, default_value, description) {
|
||||
return new Field_Configuration(null, coercion_function, () => default_value, description);
|
||||
}
|
||||
|
||||
export function boolean(default_value, description) {
|
||||
//BUG: Text representations such as "false" is still truthy here - we should have a more capable coearcing function
|
||||
return new Field_Configuration(null, Boolean, () => default_value, description);
|
||||
}
|
||||
|
||||
|
||||
export function typed_factory(coercion_function, factory_function, description) {
|
||||
return new Field_Configuration(null, coercion_function, factory_function, description);
|
||||
}
|
||||
|
||||
export function required(description) {
|
||||
return new Field_Configuration((value) => value !== undefined, null, null, 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);
|
||||
}
|
||||
82
source/data/field-configuration.mjs
Normal file
82
source/data/field-configuration.mjs
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Data_Validation_Failed, Data_Coercion_Failed, Superfluous_Data_Field } from '@efforting.tech/errors';
|
||||
|
||||
|
||||
export class Field_Configuration {
|
||||
constructor(validation_function=null, coercion_function=null, factory_function=null, expected_description=undefined) {
|
||||
Object.assign(this, { validation_function, coercion_function, factory_function, expected_description });
|
||||
}
|
||||
|
||||
check_validation(value) {
|
||||
const { validation_function } = this;
|
||||
return !validation_function || validation_function(value);
|
||||
}
|
||||
|
||||
validate(value, target=undefined) {
|
||||
const { validation_function, expected_description } = this;
|
||||
if (!this.check_validation(value)) {
|
||||
throw new Data_Validation_Failed({
|
||||
validation_function, value,
|
||||
target, expected_description,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
coerce(value, target=undefined) {
|
||||
const { coercion_function, expected_description } = this;
|
||||
try {
|
||||
return coercion_function ? coercion_function(value) : value;
|
||||
} catch (e) {
|
||||
throw new Data_Coercion_Failed({
|
||||
coercion_function, value,
|
||||
target, expected_description,
|
||||
upstream_error: e,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
load(value=undefined, target=undefined) {
|
||||
|
||||
const { factory_function } = this;
|
||||
|
||||
if ((value === undefined) && factory_function) {
|
||||
value = factory_function(target);
|
||||
}
|
||||
|
||||
const coerced_value = this.coerce(value, target);
|
||||
this.validate(coerced_value, target);
|
||||
return coerced_value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export class Schema {
|
||||
|
||||
constructor(field_schema, name=undefined) {
|
||||
Object.assign(this, { field_schema, name });
|
||||
}
|
||||
|
||||
load(value={}, target=undefined) {
|
||||
const { field_schema, name } = this;
|
||||
|
||||
for (const [value_name, value_value] of Object.entries(value)) {
|
||||
if (!field_schema[value_name]) {
|
||||
throw new Superfluous_Data_Field({
|
||||
field_value: value_value, field_name: value_name, target,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const result = {};
|
||||
const this_schema = name ? `schema "${name}"` : 'untitled schema';
|
||||
|
||||
for (const [field_name, field_config] of Object.entries(field_schema)) {
|
||||
const sub_target = { schema: this, name: field_name, config: field_config, parent_target: target, info: `Field "${field_name}" of ${this_schema}` };
|
||||
result[field_name] = field_config.load(value[field_name], sub_target);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
51
source/data/string-utilities.mjs
Normal file
51
source/data/string-utilities.mjs
Normal file
@@ -0,0 +1,51 @@
|
||||
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');
|
||||
|
||||
|
||||
export function string_has_contents(str) {
|
||||
return /\S/.test(str);
|
||||
}
|
||||
|
||||
|
||||
export function *indented_line_iterator(settings, text) {
|
||||
let line_no = settings.first_line;
|
||||
let index = 0;
|
||||
const { indention_tabulator_width } = settings;
|
||||
|
||||
switch (settings.indention_mode) {
|
||||
case Indention_Mode.symbols.TABULATORS: {
|
||||
for (const line of text.matchAll(/^(\t*)(.*)$/gm)) {
|
||||
const [raw, tabs, remaining] = line;
|
||||
yield { raw, indent: tabs.length, line: remaining, line_no: line_no++, index: index++};
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Indention_Mode.symbols.SPACES: {
|
||||
for (const line of text.matchAll(/^([ ]*)(.*)$/gm)) {
|
||||
const [raw, spaces, remaining] = line;
|
||||
|
||||
if ((spaces.length % indention_tabulator_width) !== 0) {
|
||||
throw new Error('Unaligned indention'); //TODO - proper error
|
||||
}
|
||||
yield { raw, indent: Math.floor(spaces.length / indention_tabulator_width), line: remaining, line_no: line_no++, index: index++};
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unsupported indention mode: ${settings.indention_mode}`); //TODO - proper error
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,56 @@
|
||||
import { inspect } from 'node:util';
|
||||
|
||||
// § GROUP: Configuration field errors
|
||||
|
||||
export class Data_Validation_Failed extends Error {
|
||||
constructor(data) {
|
||||
const { value, target, expected_description, validation_function, upstream_error } = data;
|
||||
const type = value === null ? 'null' : typeof value;
|
||||
|
||||
const target_info = target?.info ? inspect(target.info) : 'unknown target';
|
||||
const field_ref = target?.name ? `field "${target.name}"` : 'unknown field';
|
||||
|
||||
const expected_desc = expected_description ?? `data that would pass validation using ${validation_function}`;
|
||||
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
|
||||
super(`Data validation failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
export class Superfluous_Data_Field extends Error {
|
||||
constructor(data) {
|
||||
const { field_value, field_name, target, upstream_error } = data;
|
||||
const type = field_value === null ? 'null' : typeof field_value;
|
||||
|
||||
const target_info = target?.info ? inspect(target.info) : 'unknown target';
|
||||
|
||||
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
|
||||
super(`Data validation failed for ${target_info}. Superfluous field "${field_name}" with value of type "${type}" encountered.${upstream_error_description}`);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
export class Data_Coercion_Failed extends Error {
|
||||
constructor(data) {
|
||||
const { value, target, expected_description, coercion_function, upstream_error } = data;
|
||||
const type = value === null ? 'null' : typeof value;
|
||||
|
||||
const target_info = target?.info ? inspect(target.info) : 'unknown target';
|
||||
const field_ref = target?.name ? `field "${target.name}"` : 'unknown field';
|
||||
|
||||
const expected_desc = expected_description ?? `data that would be coerced using ${coercion_function}`;
|
||||
const upstream_error_description = upstream_error ? ` Upstream error was: ${upstream_error}` : '';
|
||||
super(`Data coercion failed for ${field_ref} of ${target_info}. Encountered data of type "${type}" while expecting "${expected_desc}".${upstream_error_description}`);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
// § GROUP: Resolving errors
|
||||
|
||||
export class Item_Unresolvable extends Error {
|
||||
constructor(data) {
|
||||
const { resolver, item } = data;
|
||||
const type = item === null ? 'null' : typeof item;
|
||||
super(`Cannot resolve item ${inspect(item)} of type "${type}" using resolver ${resolver}`);
|
||||
super(`Cannot resolve item ${inspect(item)} of type "${type}" using resolver ${resolver.constructor.name}`);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ import { Item_Unresolvable } from '@efforting.tech/errors';
|
||||
|
||||
export class Abstract_Resolver {
|
||||
resolve(item) {
|
||||
const handler = this.resolve_handler(item);
|
||||
if (!handler) {
|
||||
const result = this.resolve_handler(item);
|
||||
if (!result?.handler) {
|
||||
throw new Item_Unresolvable({ resolver: this, item });
|
||||
}
|
||||
return handler({ resolver: this, item });
|
||||
// TO DOC: Spreading result into the resulting context means there are some reserved keys we need to be mindful of to avoid clobbering them
|
||||
return result.handler({ resolver: this, item, ...result });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,17 +20,51 @@ export class Chained_Resolver extends Abstract_Resolver {
|
||||
resolve_handler(item) {
|
||||
const { chain_links } = this;
|
||||
for (const link of chain_links) {
|
||||
const handler = link.resolve_handler(item);
|
||||
if (handler) {
|
||||
return handler;
|
||||
const result = link.resolve_handler(item);
|
||||
if (result?.handler) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Predicate_Resolver extends Abstract_Resolver {
|
||||
constructor(rules=[]) {
|
||||
// NOTE: Rules should be iterable as [predicate, handler] pairs
|
||||
super();
|
||||
Object.assign(this, { rules });
|
||||
}
|
||||
|
||||
resolve_handler(item) {
|
||||
const { rules } = this;
|
||||
for (const [predicate, handler] of rules) {
|
||||
const predicate_result = predicate(item);
|
||||
if (predicate_result !== undefined) {
|
||||
return { handler, predicate_result };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class RegExp_Resolver extends Predicate_Resolver {
|
||||
constructor(rules=[]) {
|
||||
// NOTE: Rules should be iterable as [predicate, handler] pairs
|
||||
super();
|
||||
Object.assign(this, { rules: rules.map(([pattern, handler]) => {
|
||||
|
||||
const wrapped_handler = handler; //TODO
|
||||
const predicate = ((str) => str.match(pattern));
|
||||
|
||||
return [predicate, wrapped_handler];
|
||||
})});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export class Mapping_Resolver extends Abstract_Resolver {
|
||||
constructor(rules=new Map(), key_function=null) {
|
||||
super();
|
||||
Object.assign(this, { rules, key_function });
|
||||
}
|
||||
|
||||
|
||||
87
source/text/basic-tree.mjs
Normal file
87
source/text/basic-tree.mjs
Normal file
@@ -0,0 +1,87 @@
|
||||
import { string_has_contents, indented_line_iterator } from '@efforting.tech/data/string-utilities';
|
||||
import * as CF from '@efforting.tech/data/field-configuration-factories';
|
||||
import { Text_Settings } from '@efforting.tech/data/string-utilities';
|
||||
|
||||
export const Text_Tree_Settings = new CF.Schema({
|
||||
//BUG - there is currently no way (I think) to put defaults into a sub schema - this should be fixed
|
||||
text: Text_Settings,
|
||||
trim_head: CF.boolean(false, 'Trim the empty lines from the head of a node'),
|
||||
trim_tail: CF.boolean(false, 'Trim the empty lines from the tail of a node'),
|
||||
trim_lines: CF.boolean(false, 'Trim lines'),
|
||||
}, 'Text tree settings');
|
||||
|
||||
|
||||
export class Text_Tree_Node {
|
||||
constructor(text_tree_settings, line=undefined, indent=0, line_no=undefined, index=undefined, raw=undefined, parent=undefined) {
|
||||
Object.assign(this, { text_tree_settings, line, indent, line_no, index, raw, parent, children: [] });
|
||||
}
|
||||
|
||||
get has_line() {
|
||||
return string_has_contents(this.line);
|
||||
}
|
||||
|
||||
|
||||
static from_string(text_tree_settings, str) {
|
||||
|
||||
const root = new this(text_tree_settings);
|
||||
const { trim_head, trim_tail, trim_lines } = text_tree_settings;
|
||||
|
||||
// NOTE: This first Text_Node is not added to the tree, it serves as an initial cursor only.
|
||||
let current = new this(root.text_tree_settings, undefined, 0, undefined, undefined, undefined, root);
|
||||
|
||||
|
||||
for (const line_info of indented_line_iterator(text_tree_settings.text, str)) {
|
||||
|
||||
// TODO: Implement other variants than inherit-from-previous
|
||||
const indent = string_has_contents(line_info.line) ? line_info.indent : current.indent;
|
||||
|
||||
const delta_indent = indent - current.indent;
|
||||
|
||||
if (delta_indent == 0) {
|
||||
const pending = new this(current.text_tree_settings, undefined, current.indent, undefined, undefined, undefined, current.parent); // Partial insertion - same level
|
||||
if (current.parent) {
|
||||
current.parent.children.push(pending);
|
||||
}
|
||||
current = pending;
|
||||
} else if (delta_indent > 0) {
|
||||
for (let i=0; i<delta_indent; i++) {
|
||||
const pending = new this(current.text_tree_settings, undefined, current.indent + 1, undefined, undefined, undefined, current); // Partial insertion
|
||||
current.children.push(pending);
|
||||
current = pending;
|
||||
}
|
||||
} else {
|
||||
for (let i=0; i>delta_indent; i--) {
|
||||
current = current.parent;
|
||||
}
|
||||
|
||||
const pending = new this(current.text_tree_settings, undefined, current.indent, undefined, undefined, undefined, current.parent); // Partial insertion - same level
|
||||
if (current.parent) {
|
||||
current.parent.children.push(pending);
|
||||
}
|
||||
current = pending;
|
||||
}
|
||||
|
||||
// Fill in partial insertion
|
||||
Object.assign(current, {
|
||||
line: trim_lines ? line_info.line.trim() : line_info.line,
|
||||
line_no: line_info.line_no,
|
||||
index: line_info.index,
|
||||
raw: line_info.raw,
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (trim_head || trim_tail) { //TODO: Implement
|
||||
throw new Error('Trimming is not implemented'); //TODO: Proper non implemented error
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
11
tools/show-annotations.sh
Executable file
11
tools/show-annotations.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#NOTE: This tool require you to install https://gitea.efforting.tech/mikael-lovqvist/shell-utils but you could remove the last line if you don't want that.
|
||||
#Example use: From the source-directory run ../tools/show-annotations.sh
|
||||
|
||||
grep --color=always \
|
||||
-rne '//[A-Z ]*:' \
|
||||
--include "*.mjs" \
|
||||
--exclude-dir node_modules \
|
||||
| grep-mtime-sorter | ansi-trunc -l 250
|
||||
|
||||
@@ -52,9 +52,7 @@ function link_tree(src, dest) {
|
||||
}
|
||||
}
|
||||
|
||||
const workspace_manifest = {
|
||||
packages: [],
|
||||
};
|
||||
const published_packages = [];
|
||||
|
||||
const { scope, registry, author, version } = manifest;
|
||||
|
||||
@@ -62,23 +60,27 @@ const common_package_data = {
|
||||
author,
|
||||
version,
|
||||
type: 'module',
|
||||
publishConfig: {
|
||||
registry
|
||||
},
|
||||
};
|
||||
|
||||
const root_package = {
|
||||
name: path.join(scope, 'root'),
|
||||
name: path.join(scope, '_root'),
|
||||
dependencies: {},
|
||||
private: true,
|
||||
...common_package_data,
|
||||
};
|
||||
|
||||
const local_dev_package = {
|
||||
name: path.join(scope, '_dev'),
|
||||
dependencies: {},
|
||||
private: true,
|
||||
...common_package_data,
|
||||
};
|
||||
|
||||
for (const [package_name, package_data] of Object.entries(manifest.packages)) {
|
||||
const pkg = { name: package_name, ...package_data };
|
||||
|
||||
const pkg_scope_path = path.join(scope, pkg.name);
|
||||
workspace_manifest.packages.push(pkg.name);
|
||||
published_packages.push(pkg.name);
|
||||
|
||||
const pkg_dir = path.join(output_directory, pkg.name);
|
||||
const linked_sources = link_tree(pkg.path, pkg_dir).map(p => path.relative(pkg_dir, p));
|
||||
@@ -89,7 +91,7 @@ for (const [package_name, package_data] of Object.entries(manifest.packages)) {
|
||||
const exports_map = {};
|
||||
for (const file of linked_sources) {
|
||||
const name = path.basename(file, '.mjs');
|
||||
const key = name === pkg.name ? '.' : `./${name}`;
|
||||
const key = name === path.basename(pkg.name) ? '.' : `./${name}`;
|
||||
exports_map[key] = `./${file}`;
|
||||
}
|
||||
|
||||
@@ -116,20 +118,26 @@ for (const [package_name, package_data] of Object.entries(manifest.packages)) {
|
||||
writeFileSync(path.join(pkg_dir, 'package.json'), pkg_json, 'utf-8');
|
||||
//console.log({linked_sources}); // ['errors.mjs']
|
||||
|
||||
root_package.dependencies[pkg_scope_path] = version;
|
||||
|
||||
}
|
||||
|
||||
const root_pkg_json = JSON.stringify(root_package, null, ' ');
|
||||
writeFileSync(path.join(output_directory, 'package.json'), root_pkg_json, 'utf-8');
|
||||
|
||||
writeFileSync(path.join(output_directory, 'pnpm-workspace.yaml'), format_yaml(workspace_manifest), 'utf-8');
|
||||
|
||||
const publish_tool = 'pnpm publish --no-git-checks';
|
||||
const publish_script_lines = workspace_manifest.packages.map(
|
||||
pkg => `${publish_tool} ${pkg}`
|
||||
//const publish_tool = 'pnpm publish --no-git-checks';
|
||||
const publish_tool = `npm publish --registry "${registry}"`;
|
||||
const publish_script_lines = published_packages.map(
|
||||
pkg => `${publish_tool} ./${pkg}`
|
||||
);
|
||||
|
||||
const dev_stage_script_lines = published_packages.map(
|
||||
pkg => `ln -sf "${path.resolve(output_directory, pkg)}" "${path.resolve(output_directory, 'node_modules', path.join(scope, pkg))}"`
|
||||
);
|
||||
|
||||
const dev_stage_mkdir_lines = [...new Set(published_packages.map(
|
||||
pkg => `mkdir -p "${path.dirname(path.resolve(output_directory, 'node_modules', path.join(scope, pkg)))}"`
|
||||
))];
|
||||
|
||||
|
||||
|
||||
const publish_script = (
|
||||
'#!/usr/bin/env bash\n' +
|
||||
@@ -137,8 +145,19 @@ const publish_script = (
|
||||
`${publish_script_lines.join('\n')}\n`
|
||||
);
|
||||
|
||||
const dev_stage_script = (
|
||||
'#!/usr/bin/env bash\n' +
|
||||
'set -e\n' +
|
||||
`${dev_stage_mkdir_lines.join('\n')}\n` +
|
||||
`${dev_stage_script_lines.join('\n')}\n`
|
||||
);
|
||||
|
||||
|
||||
const publish_script_path = path.join(output_directory, 'publish-all.sh');
|
||||
const dev_stage_script_path = path.join(output_directory, 'local-install.sh');
|
||||
writeFileSync(publish_script_path, publish_script, 'utf-8');
|
||||
writeFileSync(dev_stage_script_path, dev_stage_script, 'utf-8');
|
||||
chmodSync(publish_script_path, 0o755);
|
||||
chmodSync(dev_stage_script_path, 0o755);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user