@20baht/engine 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/dist/approval.d.ts +15 -0
- package/dist/approval.js +18 -0
- package/dist/dates.d.ts +34 -0
- package/dist/dates.js +91 -0
- package/dist/defaults.d.ts +6 -0
- package/dist/defaults.js +37 -0
- package/dist/display.d.ts +33 -0
- package/dist/display.js +110 -0
- package/dist/engine/conditions.d.ts +27 -0
- package/dist/engine/conditions.js +123 -0
- package/dist/engine/evaluate.d.ts +8 -0
- package/dist/engine/evaluate.js +368 -0
- package/dist/engine/expressions.d.ts +3 -0
- package/dist/engine/expressions.js +70 -0
- package/dist/engine/form-session.d.ts +129 -0
- package/dist/engine/form-session.js +310 -0
- package/dist/engine/graph.d.ts +40 -0
- package/dist/engine/graph.js +59 -0
- package/dist/engine/runtime.d.ts +173 -0
- package/dist/engine/runtime.js +237 -0
- package/dist/engine/scripts.d.ts +34 -0
- package/dist/engine/scripts.js +98 -0
- package/dist/flags.d.ts +19 -0
- package/dist/flags.js +49 -0
- package/dist/form-values.d.ts +78 -0
- package/dist/form-values.js +6 -0
- package/dist/i18n.d.ts +171 -0
- package/dist/i18n.js +298 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +23 -0
- package/dist/keys.d.ts +14 -0
- package/dist/keys.js +4 -0
- package/dist/legacy.d.ts +14 -0
- package/dist/legacy.js +47 -0
- package/dist/pattern-safety.d.ts +34 -0
- package/dist/pattern-safety.js +267 -0
- package/dist/same-data.d.ts +5 -0
- package/dist/same-data.js +23 -0
- package/dist/shortcut.d.ts +44 -0
- package/dist/shortcut.js +176 -0
- package/dist/types.d.ts +541 -0
- package/dist/types.js +1 -0
- package/dist/urls.d.ts +8 -0
- package/dist/urls.js +30 -0
- package/dist/validators.d.ts +39 -0
- package/dist/validators.js +372 -0
- package/dist/views.d.ts +12 -0
- package/dist/views.js +11 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nattapong Promthong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @20baht/engine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The headless half of [bahtui](https://github.com/bank20baht/20baht): everything a form or workflow JSON means,
|
|
4
|
+
as plain TypeScript. No DOM, no Lit, no dependencies.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
bun add @20baht/engine # or npm i @20baht/engine
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
It is the same code [`@20baht/ui`](https://www.npmjs.com/package/@20baht/ui) runs in the browser:
|
|
11
|
+
`@20baht/ui/engine` re-exports this package, and
|
|
12
|
+
[`@20baht/validator`](https://www.npmjs.com/package/@20baht/validator) runs it on the server. Install it
|
|
13
|
+
on its own when there is no UI to render: a server, a worker, an agent, a renderer of your own.
|
|
14
|
+
|
|
15
|
+
## What is in it
|
|
16
|
+
|
|
17
|
+
| | |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| Schema types | `FormDefinition`, `WorkflowDefinition`, `FieldDescriptor`, `Rule`, `Registry`, ... |
|
|
20
|
+
| Rules | `evaluate(workflow, data)` → per-field and per-form state plus the cleaned data, to a fixpoint |
|
|
21
|
+
| Validation | `validateField`, `validateShownField`, the built-in validators, pattern safety |
|
|
22
|
+
| Routing | `visibleForms`, `resolveNext`, `walkPath`, `stepFieldStates` (one step's field states, for a run kept on a server) |
|
|
23
|
+
| Workflow runtime | `updateWorkflow(workflow, state, msg)` → next state and events; `replayWorkflow`, `snapshotOf` |
|
|
24
|
+
| Form session | `updateForm(input, state, msg)` → next state and events; `formData`, `formFieldState` |
|
|
25
|
+
| Client scripts | `createScriptContext`, `runScripts`, `submitScriptErrors` |
|
|
26
|
+
| The rest | flags (`applyFlags`), views, defaults, display values, dates, shortcuts, the i18n catalog (`setLocale`, `t`) |
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { evaluate, updateWorkflow, validateField } from '@20baht/engine';
|
|
30
|
+
|
|
31
|
+
const derived = evaluate(workflow, { Intro: { kind: 'company' } });
|
|
32
|
+
// derived.forms -> { Person: { visible: false }, ... }
|
|
33
|
+
// derived.fields -> { 'Company.vatId': { visible, enabled, readonly, required, computed } }
|
|
34
|
+
// derived.data -> nested output (hidden and skipped values cleared)
|
|
35
|
+
|
|
36
|
+
let run = updateWorkflow(workflow, null, { type: 'start', value: {} }).state;
|
|
37
|
+
run = updateWorkflow(workflow, run, { type: 'edit', form_name: 'Intro', key: 'kind', value: 'company' }).state;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The reference for every function is in the
|
|
41
|
+
[`@20baht/ui` README](https://github.com/bank20baht/20baht/tree/main/packages/core/ui#headless-core-any-framework-no-dom)
|
|
42
|
+
("Headless core", "A whole run, headless", "A form, headless").
|
|
43
|
+
|
|
44
|
+
## One copy
|
|
45
|
+
|
|
46
|
+
The locale catalog is module state. `@20baht/ui`, `@20baht/validator` and your own code must resolve to
|
|
47
|
+
the same copy of this package, or `setLocale` in one is not heard in the other. The packages are released
|
|
48
|
+
together under one version number and depend on the same minor, so a normal install has one copy; if you pin
|
|
49
|
+
versions by hand, keep them equal.
|
|
50
|
+
|
|
51
|
+
## Versions
|
|
52
|
+
|
|
53
|
+
Released in step with `@20baht/ui`: the same version number, always.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ApprovalDecision, FieldDescriptor, WorkflowForm } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The synthetic value-carrying fields of an approval step (spec 03 §4).
|
|
4
|
+
* The engine indexes them like authored fields so `<name>.decision` /
|
|
5
|
+
* `<name>.comment` are rule-addressable, survive `evaluate()`'s output
|
|
6
|
+
* filter, and get clear-on-skip semantics. Authored fields with the same
|
|
7
|
+
* key win (none expected in v1 — approval steps carry no authored fields).
|
|
8
|
+
*/
|
|
9
|
+
export declare function approvalFields(form: WorkflowForm): FieldDescriptor[];
|
|
10
|
+
/**
|
|
11
|
+
* Does this decision need a comment (spec 03 §4)? `required` always, `requireOnReject` on a
|
|
12
|
+
* rejection; `optional` (the default) and `hidden` never. The one gate for the client's decision
|
|
13
|
+
* and the server's check of it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function approvalCommentRequired(form: WorkflowForm, decision: ApprovalDecision | undefined): boolean;
|
package/dist/approval.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { t } from './i18n.js';
|
|
2
|
+
export function approvalFields(form) {
|
|
3
|
+
if (form.kind !== 'approval')
|
|
4
|
+
return [];
|
|
5
|
+
const has = (key) => form.fields.some((f) => f.key === key);
|
|
6
|
+
const out = [];
|
|
7
|
+
if (!has('decision')) {
|
|
8
|
+
out.push({ order: 9001, type: 'text', key: 'decision', label: t('decision') });
|
|
9
|
+
}
|
|
10
|
+
if (!has('comment')) {
|
|
11
|
+
out.push({ order: 9002, type: 'textarea', key: 'comment', label: t('comment') });
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
export function approvalCommentRequired(form, decision) {
|
|
16
|
+
const mode = form.approval?.comment ?? 'optional';
|
|
17
|
+
return mode === 'required' || (mode === 'requireOnReject' && decision === 'rejected');
|
|
18
|
+
}
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { DateMode, FieldDescriptor } from './types.js';
|
|
2
|
+
/** ISO day -> local Date at midnight, or null when malformed / not a real day. */
|
|
3
|
+
export declare function parseIsoDate(iso: unknown): Date | null;
|
|
4
|
+
export declare function formatIsoDate(date: Date): string;
|
|
5
|
+
/** ISO local datetime ('2026-04-13T14:30', seconds optional) -> local Date, or null. */
|
|
6
|
+
export declare function parseIsoDateTime(iso: unknown): Date | null;
|
|
7
|
+
/** Local Date -> ISO local datetime, minute precision (no seconds, no offset). */
|
|
8
|
+
export declare function formatIsoDateTime(date: Date): string;
|
|
9
|
+
export declare const dateModeOf: (field: FieldDescriptor) => DateMode;
|
|
10
|
+
/**
|
|
11
|
+
* Every ISO day a date value holds, whatever the mode: a single string, a
|
|
12
|
+
* `[start, end]` range (both ends only — the interior is implied) or a list.
|
|
13
|
+
* Non-string entries are dropped; shape errors are the validator's job.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isoDaysOf(value: unknown): string[];
|
|
16
|
+
/**
|
|
17
|
+
* One `disabled_dates` entry: an ISO day or an inclusive ISO interval
|
|
18
|
+
* `start/end`. Returns `[from, to]` (from === to for a single day) or null.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseDisabledEntry(entry: string): [Date, Date] | null;
|
|
21
|
+
/**
|
|
22
|
+
* True when `iso` falls on a disabled weekday or inside a disabled date /
|
|
23
|
+
* interval of the descriptor. Malformed entries never disable anything.
|
|
24
|
+
* `disabled_dates` / `disabled_weekdays` are calendar-day rules, so an ISO
|
|
25
|
+
* datetime (`enable_time` fields) is checked on its date part only.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isDateDisabled(field: FieldDescriptor, iso: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Days between a range's ends (inclusive) that a picker would step over —
|
|
30
|
+
* so a range can be rejected when a holiday sits INSIDE it. Capped so a
|
|
31
|
+
* hostile `["1900-01-01","2999-12-31"]` submission cannot spin the server;
|
|
32
|
+
* beyond the cap only the ends are checked.
|
|
33
|
+
*/
|
|
34
|
+
export declare function rangeInteriorDays(start: string, end: string, cap?: number): string[];
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const ISO_DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
2
|
+
export function parseIsoDate(iso) {
|
|
3
|
+
if (typeof iso !== 'string')
|
|
4
|
+
return null;
|
|
5
|
+
const m = ISO_DAY.exec(iso);
|
|
6
|
+
if (!m)
|
|
7
|
+
return null;
|
|
8
|
+
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
9
|
+
const date = new Date(y, mo - 1, d);
|
|
10
|
+
if (date.getFullYear() !== y || date.getMonth() !== mo - 1 || date.getDate() !== d)
|
|
11
|
+
return null;
|
|
12
|
+
return date;
|
|
13
|
+
}
|
|
14
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
15
|
+
export function formatIsoDate(date) {
|
|
16
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
17
|
+
}
|
|
18
|
+
const ISO_DATETIME = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/;
|
|
19
|
+
export function parseIsoDateTime(iso) {
|
|
20
|
+
if (typeof iso !== 'string')
|
|
21
|
+
return null;
|
|
22
|
+
const m = ISO_DATETIME.exec(iso);
|
|
23
|
+
if (!m)
|
|
24
|
+
return null;
|
|
25
|
+
const [y, mo, d, h, mi, s] = [
|
|
26
|
+
Number(m[1]),
|
|
27
|
+
Number(m[2]),
|
|
28
|
+
Number(m[3]),
|
|
29
|
+
Number(m[4]),
|
|
30
|
+
Number(m[5]),
|
|
31
|
+
Number(m[6] ?? 0),
|
|
32
|
+
];
|
|
33
|
+
const date = new Date(y, mo - 1, d, h, mi, s);
|
|
34
|
+
if (date.getFullYear() !== y ||
|
|
35
|
+
date.getMonth() !== mo - 1 ||
|
|
36
|
+
date.getDate() !== d ||
|
|
37
|
+
date.getHours() !== h ||
|
|
38
|
+
date.getMinutes() !== mi)
|
|
39
|
+
return null;
|
|
40
|
+
return date;
|
|
41
|
+
}
|
|
42
|
+
export function formatIsoDateTime(date) {
|
|
43
|
+
return `${formatIsoDate(date)}T${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
|
44
|
+
}
|
|
45
|
+
export const dateModeOf = (field) => field.date_mode ?? 'single';
|
|
46
|
+
export function isoDaysOf(value) {
|
|
47
|
+
if (typeof value === 'string')
|
|
48
|
+
return value === '' ? [] : [value];
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.filter((v) => typeof v === 'string');
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
export function parseDisabledEntry(entry) {
|
|
54
|
+
const slash = entry.indexOf('/');
|
|
55
|
+
if (slash === -1) {
|
|
56
|
+
const d = parseIsoDate(entry);
|
|
57
|
+
return d ? [d, d] : null;
|
|
58
|
+
}
|
|
59
|
+
const from = parseIsoDate(entry.slice(0, slash));
|
|
60
|
+
const to = parseIsoDate(entry.slice(slash + 1));
|
|
61
|
+
if (!from || !to)
|
|
62
|
+
return null;
|
|
63
|
+
return from <= to ? [from, to] : [to, from];
|
|
64
|
+
}
|
|
65
|
+
export function isDateDisabled(field, iso) {
|
|
66
|
+
const date = parseIsoDate(iso.slice(0, 10));
|
|
67
|
+
if (!date)
|
|
68
|
+
return false;
|
|
69
|
+
if (field.disabled_weekdays?.includes(date.getDay()))
|
|
70
|
+
return true;
|
|
71
|
+
for (const entry of field.disabled_dates ?? []) {
|
|
72
|
+
const span = parseDisabledEntry(entry);
|
|
73
|
+
if (span && date >= span[0] && date <= span[1])
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
export function rangeInteriorDays(start, end, cap = 3660) {
|
|
79
|
+
const a = parseIsoDate(start.slice(0, 10));
|
|
80
|
+
const b = parseIsoDate(end.slice(0, 10));
|
|
81
|
+
if (!a || !b || a >= b)
|
|
82
|
+
return [];
|
|
83
|
+
const out = [];
|
|
84
|
+
const cursor = new Date(a);
|
|
85
|
+
cursor.setDate(cursor.getDate() + 1);
|
|
86
|
+
while (cursor < b && out.length < cap) {
|
|
87
|
+
out.push(formatIsoDate(cursor));
|
|
88
|
+
cursor.setDate(cursor.getDate() + 1);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FieldDescriptor } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A field's default value (spec 01 §1.1): its `value` if given, else the
|
|
4
|
+
* type's empty default. Clear-on-hide/skip resets to this.
|
|
5
|
+
*/
|
|
6
|
+
export declare function defaultValue(field: FieldDescriptor): unknown;
|
package/dist/defaults.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function defaultValue(field) {
|
|
2
|
+
if (field.value !== undefined)
|
|
3
|
+
return field.value;
|
|
4
|
+
switch (field.type) {
|
|
5
|
+
case 'checkbox':
|
|
6
|
+
case 'toggle':
|
|
7
|
+
return false;
|
|
8
|
+
case 'multiselect':
|
|
9
|
+
case 'repeater':
|
|
10
|
+
case 'tags':
|
|
11
|
+
return [];
|
|
12
|
+
case 'file':
|
|
13
|
+
return field.multiple ? [] : null;
|
|
14
|
+
case 'label':
|
|
15
|
+
case 'section':
|
|
16
|
+
return undefined;
|
|
17
|
+
case 'text':
|
|
18
|
+
case 'textarea':
|
|
19
|
+
case 'markdown':
|
|
20
|
+
case 'number':
|
|
21
|
+
case 'currency':
|
|
22
|
+
case 'email':
|
|
23
|
+
case 'password':
|
|
24
|
+
case 'dropdown':
|
|
25
|
+
case 'radio':
|
|
26
|
+
case 'date':
|
|
27
|
+
case 'time':
|
|
28
|
+
case 'rating':
|
|
29
|
+
case 'range':
|
|
30
|
+
case 'reference':
|
|
31
|
+
return null;
|
|
32
|
+
default: {
|
|
33
|
+
const _exhaustive = field.type;
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FieldDescriptor, FieldOption } from './types.js';
|
|
2
|
+
export type DisplayValue = {
|
|
3
|
+
kind: 'empty';
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'text';
|
|
6
|
+
text: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'chips';
|
|
9
|
+
items: string[];
|
|
10
|
+
}
|
|
11
|
+
/** Files: each name, and a link when it is an uploaded ref with a safe url. */
|
|
12
|
+
| {
|
|
13
|
+
kind: 'files';
|
|
14
|
+
items: {
|
|
15
|
+
name: string;
|
|
16
|
+
url: string | null;
|
|
17
|
+
}[];
|
|
18
|
+
}
|
|
19
|
+
/** Repeater rows: one entry per row, each a list of `label: value` pairs. */
|
|
20
|
+
| {
|
|
21
|
+
kind: 'rows';
|
|
22
|
+
/** `reference`: the column, when `text` is a stored id its lookup can turn into a label. */
|
|
23
|
+
rows: {
|
|
24
|
+
label: string;
|
|
25
|
+
text: string;
|
|
26
|
+
reference?: FieldDescriptor;
|
|
27
|
+
}[][];
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The read-only rendering of one field's value. `options` overrides the
|
|
31
|
+
* descriptor's own list when they were resolved dynamically (`options_from`).
|
|
32
|
+
*/
|
|
33
|
+
export declare function displayValue(field: FieldDescriptor, value: unknown, options?: FieldOption[] | undefined): DisplayValue;
|
package/dist/display.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { t } from './i18n.js';
|
|
2
|
+
import { ratingMax } from './validators.js';
|
|
3
|
+
import { isSafeUrl } from './urls.js';
|
|
4
|
+
const isEmpty = (value) => value === null ||
|
|
5
|
+
value === undefined ||
|
|
6
|
+
value === '' ||
|
|
7
|
+
(Array.isArray(value) && value.length === 0);
|
|
8
|
+
const labelFor = (options, key) => {
|
|
9
|
+
const found = options?.find((o) => o.key === String(key));
|
|
10
|
+
return found ? found.label : String(key);
|
|
11
|
+
};
|
|
12
|
+
const fileName = (file) => {
|
|
13
|
+
if (file && typeof file === 'object' && 'name' in file) {
|
|
14
|
+
return String(file.name);
|
|
15
|
+
}
|
|
16
|
+
return String(file);
|
|
17
|
+
};
|
|
18
|
+
const formatDate = (raw) => {
|
|
19
|
+
const asDate = new Date(raw);
|
|
20
|
+
if (Number.isNaN(asDate.getTime()))
|
|
21
|
+
return raw;
|
|
22
|
+
return /^\d{4}-\d{2}-\d{2}$/.test(raw)
|
|
23
|
+
? new Date(raw + 'T00:00:00').toLocaleDateString()
|
|
24
|
+
: asDate.toLocaleString();
|
|
25
|
+
};
|
|
26
|
+
const emptyDisplay = (field) => field.type === 'dropdown' && field.empty_label
|
|
27
|
+
? { kind: 'text', text: field.empty_label }
|
|
28
|
+
: { kind: 'empty' };
|
|
29
|
+
export function displayValue(field, value, options = field.options) {
|
|
30
|
+
if (isEmpty(value))
|
|
31
|
+
return emptyDisplay(field);
|
|
32
|
+
switch (field.type) {
|
|
33
|
+
case 'checkbox':
|
|
34
|
+
case 'toggle':
|
|
35
|
+
return { kind: 'text', text: value ? t('yes') : t('no') };
|
|
36
|
+
case 'dropdown':
|
|
37
|
+
case 'radio':
|
|
38
|
+
return { kind: 'text', text: labelFor(options, value) };
|
|
39
|
+
case 'multiselect':
|
|
40
|
+
case 'tags': {
|
|
41
|
+
const keys = Array.isArray(value) ? value : [value];
|
|
42
|
+
return { kind: 'chips', items: keys.map((k) => labelFor(options, k)) };
|
|
43
|
+
}
|
|
44
|
+
case 'date': {
|
|
45
|
+
if (!Array.isArray(value))
|
|
46
|
+
return { kind: 'text', text: formatDate(String(value)) };
|
|
47
|
+
const days = value.map((v) => formatDate(String(v)));
|
|
48
|
+
return field.date_mode === 'range' && days.length === 2
|
|
49
|
+
? { kind: 'text', text: `${days[0]} – ${days[1]}` }
|
|
50
|
+
: { kind: 'chips', items: days };
|
|
51
|
+
}
|
|
52
|
+
case 'file': {
|
|
53
|
+
const files = Array.isArray(value) ? value : [value];
|
|
54
|
+
return {
|
|
55
|
+
kind: 'files',
|
|
56
|
+
items: files.map((file) => {
|
|
57
|
+
const raw = file !== null && typeof file === 'object' && 'url' in file ? file.url : undefined;
|
|
58
|
+
const url = typeof raw === 'string' ? raw : null;
|
|
59
|
+
return { name: fileName(file), url: url !== null && isSafeUrl(url) ? url : null };
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
case 'repeater': {
|
|
64
|
+
const rows = Array.isArray(value) ? value : [];
|
|
65
|
+
const subFields = (field.fields ?? []).filter((f) => f.type !== 'label' && f.type !== 'section' && !f.hidden);
|
|
66
|
+
return {
|
|
67
|
+
kind: 'rows',
|
|
68
|
+
rows: rows.map((row) => subFields.map((f) => {
|
|
69
|
+
const cell = row !== null && typeof row === 'object'
|
|
70
|
+
? row[f.key]
|
|
71
|
+
: undefined;
|
|
72
|
+
const sub = displayValue(f, cell);
|
|
73
|
+
const text = sub.kind === 'text'
|
|
74
|
+
? sub.text
|
|
75
|
+
: sub.kind === 'chips'
|
|
76
|
+
? sub.items.join(', ')
|
|
77
|
+
: sub.kind === 'files'
|
|
78
|
+
? sub.items.map((f) => f.name).join(', ')
|
|
79
|
+
: sub.kind === 'rows'
|
|
80
|
+
? t('rowCount', { n: sub.rows.length })
|
|
81
|
+
: t('emptyValue');
|
|
82
|
+
return {
|
|
83
|
+
label: f.label ?? f.key,
|
|
84
|
+
text,
|
|
85
|
+
...(f.type === 'reference' && sub.kind === 'text' ? { reference: f } : {}),
|
|
86
|
+
};
|
|
87
|
+
})),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
case 'rating':
|
|
91
|
+
return { kind: 'text', text: t('ratingValue', { n: String(value), max: ratingMax(field) }) };
|
|
92
|
+
case 'range':
|
|
93
|
+
case 'reference':
|
|
94
|
+
case 'text':
|
|
95
|
+
case 'textarea':
|
|
96
|
+
case 'markdown':
|
|
97
|
+
case 'number':
|
|
98
|
+
case 'currency':
|
|
99
|
+
case 'email':
|
|
100
|
+
case 'password':
|
|
101
|
+
case 'time':
|
|
102
|
+
case 'label':
|
|
103
|
+
case 'section':
|
|
104
|
+
return { kind: 'text', text: String(value) };
|
|
105
|
+
default: {
|
|
106
|
+
const _exhaustive = field.type;
|
|
107
|
+
return { kind: 'text', text: String(value) };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Condition, NestedData } from '../types.js';
|
|
2
|
+
/** The repeater row a rule is being evaluated for (spec 02 §2a). */
|
|
3
|
+
export interface RowScope {
|
|
4
|
+
form: string;
|
|
5
|
+
repeater: string;
|
|
6
|
+
index: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RowRef {
|
|
9
|
+
form: string;
|
|
10
|
+
repeater: string;
|
|
11
|
+
sub: string;
|
|
12
|
+
}
|
|
13
|
+
/** Split a row-scoped ref, or `null` when it is a plain `Form.key` reference. */
|
|
14
|
+
export declare function parseRowRef(ref: string): RowRef | null;
|
|
15
|
+
/**
|
|
16
|
+
* Read a "formName.fieldKey" reference out of nested data. With a `scope`, a
|
|
17
|
+
* row-scoped ref (`Form.repeater[].sub`) reads that sub-field of the scoped row.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readRef(data: NestedData, ref: string, scope?: RowScope): unknown;
|
|
20
|
+
/**
|
|
21
|
+
* Ordering used by rule conditions AND the `compare` validator: numeric when
|
|
22
|
+
* both sides coerce, date-aware, else lexicographic. Null = not comparable.
|
|
23
|
+
*/
|
|
24
|
+
export declare function compareValues(a: unknown, b: unknown): number | null;
|
|
25
|
+
export declare function evalCondition(cond: Condition | undefined, data: NestedData, scope?: RowScope): boolean;
|
|
26
|
+
/** Every row-scoped ref a condition mentions (`field` and `value_field`). */
|
|
27
|
+
export declare function conditionRowRefs(cond: Condition | undefined): RowRef[];
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
const ROW_REF = /^([^.[\]]+)\.([^.[\]]+)\[\]\.([^.[\]]+)$/;
|
|
2
|
+
export function parseRowRef(ref) {
|
|
3
|
+
const m = ROW_REF.exec(ref);
|
|
4
|
+
return m ? { form: m[1], repeater: m[2], sub: m[3] } : null;
|
|
5
|
+
}
|
|
6
|
+
export function readRef(data, ref, scope) {
|
|
7
|
+
const row = parseRowRef(ref);
|
|
8
|
+
if (row) {
|
|
9
|
+
if (scope?.form !== row.form || scope.repeater !== row.repeater)
|
|
10
|
+
return undefined;
|
|
11
|
+
const rows = data[row.form]?.[row.repeater];
|
|
12
|
+
const item = Array.isArray(rows) ? rows[scope.index] : undefined;
|
|
13
|
+
return item !== null && typeof item === 'object' && Object.hasOwn(item, row.sub)
|
|
14
|
+
? item[row.sub]
|
|
15
|
+
: undefined;
|
|
16
|
+
}
|
|
17
|
+
const dot = ref.indexOf('.');
|
|
18
|
+
if (dot === -1)
|
|
19
|
+
return undefined;
|
|
20
|
+
return data[ref.slice(0, dot)]?.[ref.slice(dot + 1)];
|
|
21
|
+
}
|
|
22
|
+
function isEmptyValue(v) {
|
|
23
|
+
if (v === null || v === undefined)
|
|
24
|
+
return true;
|
|
25
|
+
if (typeof v === 'string')
|
|
26
|
+
return v === '';
|
|
27
|
+
if (Array.isArray(v))
|
|
28
|
+
return v.length === 0;
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
function compareDates(a, b) {
|
|
32
|
+
const ta = new Date(a).getTime();
|
|
33
|
+
const tb = new Date(b).getTime();
|
|
34
|
+
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
35
|
+
return null;
|
|
36
|
+
return ta - tb;
|
|
37
|
+
}
|
|
38
|
+
function compareStrings(a, b) {
|
|
39
|
+
const na = Number(a);
|
|
40
|
+
const nb = Number(b);
|
|
41
|
+
if (a !== '' && b !== '' && !Number.isNaN(na) && !Number.isNaN(nb))
|
|
42
|
+
return na - nb;
|
|
43
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
44
|
+
}
|
|
45
|
+
export function compareValues(a, b) {
|
|
46
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
47
|
+
return a - b;
|
|
48
|
+
if (a instanceof Date || b instanceof Date)
|
|
49
|
+
return compareDates(a, b);
|
|
50
|
+
if (typeof a === 'string' && typeof b === 'string')
|
|
51
|
+
return compareStrings(a, b);
|
|
52
|
+
const na = Number(a);
|
|
53
|
+
const nb = Number(b);
|
|
54
|
+
if (Number.isNaN(na) || Number.isNaN(nb))
|
|
55
|
+
return null;
|
|
56
|
+
return na - nb;
|
|
57
|
+
}
|
|
58
|
+
function orders(left, right, test) {
|
|
59
|
+
if (isEmptyValue(left) || isEmptyValue(right))
|
|
60
|
+
return false;
|
|
61
|
+
const c = compareValues(left, right);
|
|
62
|
+
return c !== null && test(c);
|
|
63
|
+
}
|
|
64
|
+
function evalLeaf(cond, data, scope) {
|
|
65
|
+
const left = readRef(data, cond.field, scope);
|
|
66
|
+
const right = cond.value_field !== undefined ? readRef(data, cond.value_field, scope) : cond.value;
|
|
67
|
+
switch (cond.op) {
|
|
68
|
+
case 'eq':
|
|
69
|
+
return left === right;
|
|
70
|
+
case 'ne':
|
|
71
|
+
return left !== right;
|
|
72
|
+
case 'gt':
|
|
73
|
+
return orders(left, right, (c) => c > 0);
|
|
74
|
+
case 'gte':
|
|
75
|
+
return orders(left, right, (c) => c >= 0);
|
|
76
|
+
case 'lt':
|
|
77
|
+
return orders(left, right, (c) => c < 0);
|
|
78
|
+
case 'lte':
|
|
79
|
+
return orders(left, right, (c) => c <= 0);
|
|
80
|
+
case 'in':
|
|
81
|
+
return Array.isArray(right) && right.includes(left);
|
|
82
|
+
case 'notIn':
|
|
83
|
+
return Array.isArray(right) && !right.includes(left);
|
|
84
|
+
case 'contains':
|
|
85
|
+
if (Array.isArray(left))
|
|
86
|
+
return left.includes(right);
|
|
87
|
+
return typeof left === 'string' && left.includes(String(right));
|
|
88
|
+
case 'notContains':
|
|
89
|
+
if (Array.isArray(left))
|
|
90
|
+
return !left.includes(right);
|
|
91
|
+
return typeof left === 'string' && !left.includes(String(right));
|
|
92
|
+
case 'startsWith':
|
|
93
|
+
return typeof left === 'string' && left.startsWith(String(right));
|
|
94
|
+
case 'endsWith':
|
|
95
|
+
return typeof left === 'string' && left.endsWith(String(right));
|
|
96
|
+
case 'isEmpty':
|
|
97
|
+
return isEmptyValue(left);
|
|
98
|
+
case 'isNotEmpty':
|
|
99
|
+
return !isEmptyValue(left);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export function evalCondition(cond, data, scope) {
|
|
103
|
+
if (cond === undefined)
|
|
104
|
+
return true;
|
|
105
|
+
if ('and' in cond)
|
|
106
|
+
return cond.and.every((c) => evalCondition(c, data, scope));
|
|
107
|
+
if ('or' in cond)
|
|
108
|
+
return cond.or.some((c) => evalCondition(c, data, scope));
|
|
109
|
+
if ('not' in cond)
|
|
110
|
+
return !evalCondition(cond.not, data, scope);
|
|
111
|
+
return evalLeaf(cond, data, scope);
|
|
112
|
+
}
|
|
113
|
+
export function conditionRowRefs(cond) {
|
|
114
|
+
if (cond === undefined)
|
|
115
|
+
return [];
|
|
116
|
+
if ('and' in cond)
|
|
117
|
+
return cond.and.flatMap(conditionRowRefs);
|
|
118
|
+
if ('or' in cond)
|
|
119
|
+
return cond.or.flatMap(conditionRowRefs);
|
|
120
|
+
if ('not' in cond)
|
|
121
|
+
return conditionRowRefs(cond.not);
|
|
122
|
+
return [parseRowRef(cond.field), cond.value_field ? parseRowRef(cond.value_field) : null].filter((r) => r !== null);
|
|
123
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Derived, NestedData, WorkflowDefinition } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The rules engine (spec 02 §5). Pure: same inputs -> same Derived.
|
|
4
|
+
* Reactive fixpoint: reset -> eval when -> apply (priority-resolved) ->
|
|
5
|
+
* clear hidden/skipped to defaults -> setValue -> repeat until stable
|
|
6
|
+
* (cycle guard MAX_PASSES; overflow surfaces an error, never hangs).
|
|
7
|
+
*/
|
|
8
|
+
export declare function evaluate(workflow: WorkflowDefinition, input: NestedData): Derived;
|