@20baht/engine 0.0.0-stage → 0.1.1

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/dist/approval.d.ts +15 -0
  4. package/dist/approval.js +18 -0
  5. package/dist/dates.d.ts +34 -0
  6. package/dist/dates.js +91 -0
  7. package/dist/defaults.d.ts +6 -0
  8. package/dist/defaults.js +37 -0
  9. package/dist/display.d.ts +33 -0
  10. package/dist/display.js +110 -0
  11. package/dist/engine/conditions.d.ts +27 -0
  12. package/dist/engine/conditions.js +123 -0
  13. package/dist/engine/evaluate.d.ts +8 -0
  14. package/dist/engine/evaluate.js +368 -0
  15. package/dist/engine/expressions.d.ts +3 -0
  16. package/dist/engine/expressions.js +70 -0
  17. package/dist/engine/form-session.d.ts +135 -0
  18. package/dist/engine/form-session.js +313 -0
  19. package/dist/engine/graph.d.ts +40 -0
  20. package/dist/engine/graph.js +59 -0
  21. package/dist/engine/runtime.d.ts +194 -0
  22. package/dist/engine/runtime.js +268 -0
  23. package/dist/engine/scripts.d.ts +34 -0
  24. package/dist/engine/scripts.js +98 -0
  25. package/dist/flags.d.ts +19 -0
  26. package/dist/flags.js +49 -0
  27. package/dist/form-values.d.ts +78 -0
  28. package/dist/form-values.js +6 -0
  29. package/dist/i18n.d.ts +177 -0
  30. package/dist/i18n.js +310 -0
  31. package/dist/index.d.ts +25 -0
  32. package/dist/index.js +23 -0
  33. package/dist/keys.d.ts +14 -0
  34. package/dist/keys.js +4 -0
  35. package/dist/legacy.d.ts +14 -0
  36. package/dist/legacy.js +47 -0
  37. package/dist/pattern-safety.d.ts +34 -0
  38. package/dist/pattern-safety.js +267 -0
  39. package/dist/same-data.d.ts +5 -0
  40. package/dist/same-data.js +23 -0
  41. package/dist/shortcut.d.ts +44 -0
  42. package/dist/shortcut.js +176 -0
  43. package/dist/types.d.ts +541 -0
  44. package/dist/types.js +1 -0
  45. package/dist/urls.d.ts +8 -0
  46. package/dist/urls.js +30 -0
  47. package/dist/validators.d.ts +39 -0
  48. package/dist/validators.js +372 -0
  49. package/dist/views.d.ts +12 -0
  50. package/dist/views.js +11 -0
  51. package/package.json +52 -4
@@ -0,0 +1,267 @@
1
+ const MAX_SAFE_REPEAT = 2;
2
+ const SAMPLE = [
3
+ ...Array.from({ length: 128 }, (_, i) => String.fromCharCode(i)),
4
+ '\u00A0',
5
+ '\u00E9',
6
+ '\u0E01',
7
+ '\u200B',
8
+ '\u2028',
9
+ '\u4E2D',
10
+ '\uD83D',
11
+ ];
12
+ const ALL_CHARS = (1n << BigInt(SAMPLE.length)) - 1n;
13
+ function charsOf(atomSource) {
14
+ if (/^\\[1-9]/.test(atomSource))
15
+ return ALL_CHARS;
16
+ let re;
17
+ try {
18
+ re = new RegExp(`^(?:${atomSource})$`);
19
+ }
20
+ catch {
21
+ return ALL_CHARS;
22
+ }
23
+ let set = 0n;
24
+ SAMPLE.forEach((ch, i) => {
25
+ if (re.test(ch))
26
+ set |= 1n << BigInt(i);
27
+ });
28
+ return set;
29
+ }
30
+ function atomItem(source) {
31
+ const chars = charsOf(source);
32
+ return { min: 1, max: 1, zeroWidth: false, chars, first: chars, nullable: false };
33
+ }
34
+ const ZERO_WIDTH = { min: 1, max: 1, zeroWidth: true, chars: 0n, first: 0n, nullable: true };
35
+ function firstOf(items) {
36
+ let first = 0n;
37
+ for (const item of items) {
38
+ if (item.zeroWidth)
39
+ continue;
40
+ first |= item.first;
41
+ if (!item.nullable)
42
+ return { first, nullable: false };
43
+ }
44
+ return { first, nullable: true };
45
+ }
46
+ class Parser {
47
+ constructor(src) {
48
+ this.src = src;
49
+ this.i = 0;
50
+ }
51
+ parseAlts() {
52
+ const alts = [];
53
+ let start = this.i;
54
+ let items = [];
55
+ while (this.i < this.src.length && this.src[this.i] !== ')') {
56
+ if (this.src[this.i] === '|') {
57
+ alts.push({ items, source: this.src.slice(start, this.i) });
58
+ this.i++;
59
+ start = this.i;
60
+ items = [];
61
+ continue;
62
+ }
63
+ items.push(this.quantify(this.atom()));
64
+ }
65
+ alts.push({ items, source: this.src.slice(start, this.i) });
66
+ return alts;
67
+ }
68
+ atom() {
69
+ const start = this.i;
70
+ const ch = this.src[this.i++];
71
+ if (ch === '\\') {
72
+ const next = this.src[this.i++];
73
+ if (next === 'b' || next === 'B')
74
+ return ZERO_WIDTH;
75
+ this.skipEscapeBody(next);
76
+ return atomItem(this.src.slice(start, this.i));
77
+ }
78
+ if (ch === '[') {
79
+ this.skipClass();
80
+ return atomItem(this.src.slice(start, this.i));
81
+ }
82
+ if (ch === '(')
83
+ return this.group();
84
+ if (ch === '^' || ch === '$')
85
+ return ZERO_WIDTH;
86
+ return atomItem(this.src.slice(start, this.i));
87
+ }
88
+ skipEscapeBody(letter) {
89
+ const rest = this.src.slice(this.i);
90
+ const m = letter === 'x'
91
+ ? /^[\dA-Fa-f]{2}/.exec(rest)
92
+ : letter === 'u'
93
+ ? /^[\dA-Fa-f]{4}/.exec(rest)
94
+ : letter === 'c'
95
+ ? /^[A-Za-z]/.exec(rest)
96
+ : null;
97
+ if (m)
98
+ this.i += m[0].length;
99
+ }
100
+ skipClass() {
101
+ if (this.src[this.i] === '^')
102
+ this.i++;
103
+ if (this.src[this.i] === ']')
104
+ this.i++;
105
+ while (this.i < this.src.length && this.src[this.i] !== ']') {
106
+ this.i += this.src[this.i] === '\\' ? 2 : 1;
107
+ }
108
+ this.i++;
109
+ }
110
+ group() {
111
+ let zeroWidth = false;
112
+ if (this.src[this.i] === '?') {
113
+ const rest = this.src.slice(this.i + 1);
114
+ if (/^(?:[=!]|<[=!])/.test(rest)) {
115
+ zeroWidth = true;
116
+ this.i += rest.startsWith('<') ? 3 : 2;
117
+ }
118
+ else if (rest.startsWith(':')) {
119
+ this.i += 2;
120
+ }
121
+ else if (rest.startsWith('<')) {
122
+ this.i = this.src.indexOf('>', this.i) + 1;
123
+ }
124
+ }
125
+ const alts = this.parseAlts();
126
+ this.i++;
127
+ if (zeroWidth)
128
+ return { ...ZERO_WIDTH, alts };
129
+ let chars = 0n;
130
+ let first = 0n;
131
+ let nullable = false;
132
+ for (const alt of alts) {
133
+ for (const item of alt.items)
134
+ chars |= item.chars;
135
+ const f = firstOf(alt.items);
136
+ first |= f.first;
137
+ nullable ||= f.nullable;
138
+ }
139
+ return { min: 1, max: 1, zeroWidth: false, alts, chars, first, nullable };
140
+ }
141
+ quantify(item) {
142
+ const ch = this.src[this.i];
143
+ let bounds;
144
+ if (ch === '*')
145
+ bounds = [0, Infinity];
146
+ else if (ch === '+')
147
+ bounds = [1, Infinity];
148
+ else if (ch === '?')
149
+ bounds = [0, 1];
150
+ if (bounds) {
151
+ this.i++;
152
+ }
153
+ else {
154
+ const m = /^\{(\d+)(,(\d*))?\}/.exec(this.src.slice(this.i));
155
+ if (!m)
156
+ return item;
157
+ this.i += m[0].length;
158
+ const lo = Number(m[1]);
159
+ bounds = [lo, m[2] === undefined ? lo : m[3] === '' ? Infinity : Number(m[3])];
160
+ }
161
+ if (this.src[this.i] === '?')
162
+ this.i++;
163
+ return {
164
+ ...item,
165
+ min: item.min * bounds[0],
166
+ max: item.max * bounds[1],
167
+ nullable: item.nullable || bounds[0] === 0,
168
+ };
169
+ }
170
+ }
171
+ function varies(item) {
172
+ if (item.zeroWidth)
173
+ return false;
174
+ if (item.max > item.min)
175
+ return true;
176
+ return item.alts?.some(isAmbiguous) ?? false;
177
+ }
178
+ function isAmbiguous(branch) {
179
+ const consuming = branch.items.filter((item) => !item.zeroWidth);
180
+ const mandatory = consuming.filter((item) => item.min > 0);
181
+ if (mandatory.length > 0)
182
+ return mandatory.every(varies);
183
+ const [only] = consuming;
184
+ if (consuming.length === 1 && only)
185
+ return only.max > 1 || (only.alts?.some(isAmbiguous) ?? false);
186
+ return consuming.some(varies);
187
+ }
188
+ function hasDuplicateAlts(alts) {
189
+ return new Set(alts.map((alt) => alt.source)).size < alts.length;
190
+ }
191
+ function altsOverlap(alts) {
192
+ if (alts.length < 2)
193
+ return false;
194
+ let seen = 0n;
195
+ for (const alt of alts) {
196
+ const f = firstOf(alt.items);
197
+ if (f.nullable || (seen & f.first) !== 0n)
198
+ return true;
199
+ seen |= f.first;
200
+ }
201
+ return false;
202
+ }
203
+ function swallowsFollower(group, alt) {
204
+ const consuming = alt.items.filter((item) => !item.zeroWidth).length;
205
+ return alt.items.some((item, i) => {
206
+ if (item.zeroWidth || item.max <= item.min)
207
+ return false;
208
+ const rest = firstOf(alt.items.slice(i + 1));
209
+ if ((item.chars & rest.first) !== 0n)
210
+ return true;
211
+ if (!rest.nullable || (item.max <= 1 && consuming === 1))
212
+ return false;
213
+ return (item.chars & group.first) !== 0n;
214
+ });
215
+ }
216
+ const MAX_OVERLAPPING_RUNS = 2;
217
+ function hasOverlappingRuns(branch) {
218
+ let runs = 0;
219
+ let shared = 0n;
220
+ for (const item of branch.items) {
221
+ if (item.zeroWidth)
222
+ continue;
223
+ if (item.max !== Infinity) {
224
+ if (!item.nullable)
225
+ runs = 0;
226
+ continue;
227
+ }
228
+ if (runs > 0 && (shared & item.chars) !== 0n) {
229
+ runs++;
230
+ shared &= item.chars;
231
+ }
232
+ else {
233
+ runs = 1;
234
+ shared = item.chars;
235
+ }
236
+ if (runs > MAX_OVERLAPPING_RUNS)
237
+ return true;
238
+ }
239
+ return false;
240
+ }
241
+ function itemIsUnsafe(item) {
242
+ if (!item.alts)
243
+ return false;
244
+ const alts = item.alts;
245
+ if (item.max > MAX_SAFE_REPEAT &&
246
+ (alts.some(isAmbiguous) ||
247
+ hasDuplicateAlts(alts) ||
248
+ altsOverlap(alts) ||
249
+ alts.some((alt) => swallowsFollower(item, alt)))) {
250
+ return true;
251
+ }
252
+ return alts.some(branchIsUnsafe);
253
+ }
254
+ function branchIsUnsafe(branch) {
255
+ return hasOverlappingRuns(branch) || branch.items.some(itemIsUnsafe);
256
+ }
257
+ export function isUnsafePattern(source) {
258
+ try {
259
+ new RegExp(source);
260
+ }
261
+ catch {
262
+ return false;
263
+ }
264
+ const parser = new Parser(source);
265
+ const alts = parser.parseAlts();
266
+ return alts.some(branchIsUnsafe);
267
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Equal by content: plain objects and arrays compared key by key, dates by time, anything else
3
+ * (a `File`, a class instance) by identity. For a host re-feeding `value` with a fresh copy.
4
+ */
5
+ export declare function sameData(a: unknown, b: unknown): boolean;
@@ -0,0 +1,23 @@
1
+ export function sameData(a, b) {
2
+ if (Object.is(a, b))
3
+ return true;
4
+ if (a instanceof Date && b instanceof Date)
5
+ return a.getTime() === b.getTime();
6
+ if (Array.isArray(a) || Array.isArray(b)) {
7
+ return (Array.isArray(a) &&
8
+ Array.isArray(b) &&
9
+ a.length === b.length &&
10
+ a.every((v, i) => sameData(v, b[i])));
11
+ }
12
+ if (!isPlain(a) || !isPlain(b))
13
+ return false;
14
+ const keys = Object.keys(a);
15
+ return (keys.length === Object.keys(b).length &&
16
+ keys.every((k) => Object.hasOwn(b, k) && sameData(a[k], b[k])));
17
+ }
18
+ function isPlain(v) {
19
+ if (typeof v !== 'object' || v === null)
20
+ return false;
21
+ const proto = Object.getPrototypeOf(v);
22
+ return proto === Object.prototype || proto === null;
23
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Keyboard shortcut specs — pure parsing, display and matching (no DOM).
3
+ *
4
+ * A spec is `modifier+…+key`, case-insensitive: `alt+q`, `shift+alt+s`, `mod+enter`.
5
+ * Modifiers: `ctrl`/`control`, `alt`/`option`, `shift`, `meta`/`cmd`/`command`, and
6
+ * `mod` (⌘ on Mac, Ctrl elsewhere). The key is one letter, digit, punctuation mark,
7
+ * or a named key (`enter`, `escape`, `space`, `tab`, `backspace`, `delete`,
8
+ * `arrowup`…, `home`, `end`, `pageup`, `pagedown`, `f1`…`f12`).
9
+ *
10
+ * Letters, digits and punctuation match by PHYSICAL key (`KeyboardEvent.code`), so
11
+ * ⌥Q on a Mac (which types "œ") and a Thai layout (which types "ๆ") still match `alt+q`.
12
+ * Named keys match by `KeyboardEvent.key`.
13
+ */
14
+ export interface ParsedShortcut {
15
+ /** Lower-cased key token: `q`, `1`, `/`, `enter`, `f2`. */
16
+ key: string;
17
+ ctrl: boolean;
18
+ alt: boolean;
19
+ shift: boolean;
20
+ meta: boolean;
21
+ /** ⌘ on Mac, Ctrl elsewhere — resolved at match/format time. */
22
+ mod: boolean;
23
+ }
24
+ /** The slice of `KeyboardEvent` matching reads; plain objects work too. */
25
+ export interface ShortcutKeyEvent {
26
+ key: string;
27
+ code: string;
28
+ altKey: boolean;
29
+ ctrlKey: boolean;
30
+ metaKey: boolean;
31
+ shiftKey: boolean;
32
+ }
33
+ export interface ShortcutPlatform {
34
+ /** Apple platform: `mod` = ⌘ and display uses ⌃⌥⇧⌘ glyphs. */
35
+ mac: boolean;
36
+ }
37
+ /** Parse a spec; `null` when it has no key, two keys, or an unknown token. */
38
+ export declare function parseShortcut(spec: string): ParsedShortcut | null;
39
+ /** Display text: `⌥⇧S` on Mac (⌃⌥⇧⌘ order), `Alt+Shift+S` elsewhere; `''` if unparseable. */
40
+ export declare function formatShortcut(spec: string, { mac }: ShortcutPlatform): string;
41
+ /** Value for `aria-keyshortcuts` (`Alt+Shift+S`, `Control+S`, `Meta+Enter`); `''` if unparseable. */
42
+ export declare function ariaKeyshortcuts(spec: string, { mac }: ShortcutPlatform): string;
43
+ /** True when the event is exactly this shortcut (same modifier set, same key). */
44
+ export declare function matchShortcut(e: ShortcutKeyEvent, spec: string | ParsedShortcut, { mac }: ShortcutPlatform): boolean;
@@ -0,0 +1,176 @@
1
+ const MODIFIERS = {
2
+ ctrl: 'ctrl',
3
+ control: 'ctrl',
4
+ alt: 'alt',
5
+ option: 'alt',
6
+ opt: 'alt',
7
+ shift: 'shift',
8
+ meta: 'meta',
9
+ cmd: 'meta',
10
+ command: 'meta',
11
+ mod: 'mod',
12
+ };
13
+ const PUNCTUATION_CODE = {
14
+ '/': 'Slash',
15
+ ',': 'Comma',
16
+ '.': 'Period',
17
+ ';': 'Semicolon',
18
+ "'": 'Quote',
19
+ '[': 'BracketLeft',
20
+ ']': 'BracketRight',
21
+ '\\': 'Backslash',
22
+ '-': 'Minus',
23
+ '=': 'Equal',
24
+ '`': 'Backquote',
25
+ };
26
+ const NAMED_KEY = {
27
+ enter: 'Enter',
28
+ return: 'Enter',
29
+ escape: 'Escape',
30
+ esc: 'Escape',
31
+ space: ' ',
32
+ tab: 'Tab',
33
+ backspace: 'Backspace',
34
+ delete: 'Delete',
35
+ del: 'Delete',
36
+ arrowup: 'ArrowUp',
37
+ arrowdown: 'ArrowDown',
38
+ arrowleft: 'ArrowLeft',
39
+ arrowright: 'ArrowRight',
40
+ up: 'ArrowUp',
41
+ down: 'ArrowDown',
42
+ left: 'ArrowLeft',
43
+ right: 'ArrowRight',
44
+ home: 'Home',
45
+ end: 'End',
46
+ pageup: 'PageUp',
47
+ pagedown: 'PageDown',
48
+ };
49
+ const MAC_KEY_GLYPH = {
50
+ Enter: '↵',
51
+ Escape: 'Esc',
52
+ ' ': 'Space',
53
+ Tab: '⇥',
54
+ Backspace: '⌫',
55
+ Delete: '⌦',
56
+ ArrowUp: '↑',
57
+ ArrowDown: '↓',
58
+ ArrowLeft: '←',
59
+ ArrowRight: '→',
60
+ Home: 'Home',
61
+ End: 'End',
62
+ PageUp: 'PgUp',
63
+ PageDown: 'PgDn',
64
+ };
65
+ const WORD_KEY = {
66
+ ...MAC_KEY_GLYPH,
67
+ Enter: 'Enter',
68
+ ' ': 'Space',
69
+ Tab: 'Tab',
70
+ Backspace: 'Backspace',
71
+ Delete: 'Del',
72
+ };
73
+ const ARIA_KEY = { ' ': 'Space' };
74
+ function isKeyToken(token) {
75
+ return (/^[a-z0-9]$/.test(token) ||
76
+ token in PUNCTUATION_CODE ||
77
+ token in NAMED_KEY ||
78
+ /^f([1-9]|1[0-2])$/.test(token));
79
+ }
80
+ export function parseShortcut(spec) {
81
+ const tokens = spec
82
+ .trim()
83
+ .toLowerCase()
84
+ .split('+')
85
+ .map((t) => t.trim());
86
+ const out = {
87
+ key: '',
88
+ ctrl: false,
89
+ alt: false,
90
+ shift: false,
91
+ meta: false,
92
+ mod: false,
93
+ };
94
+ for (const token of tokens) {
95
+ const modifier = MODIFIERS[token];
96
+ if (modifier)
97
+ out[modifier] = true;
98
+ else if (!out.key && isKeyToken(token))
99
+ out.key = token;
100
+ else
101
+ return null;
102
+ }
103
+ return out.key ? out : null;
104
+ }
105
+ function eventKeyOf(token) {
106
+ const named = NAMED_KEY[token];
107
+ if (named)
108
+ return named;
109
+ if (/^f\d+$/.test(token))
110
+ return token.toUpperCase();
111
+ return token;
112
+ }
113
+ function resolved(p, mac) {
114
+ return { ctrl: p.ctrl || (p.mod && !mac), meta: p.meta || (p.mod && mac) };
115
+ }
116
+ function keyLabel(token, mac) {
117
+ const key = eventKeyOf(token);
118
+ const table = mac ? MAC_KEY_GLYPH : WORD_KEY;
119
+ return table[key] ?? key.toUpperCase();
120
+ }
121
+ export function formatShortcut(spec, { mac }) {
122
+ const p = parseShortcut(spec);
123
+ if (!p)
124
+ return '';
125
+ const { ctrl, meta } = resolved(p, mac);
126
+ const label = keyLabel(p.key, mac);
127
+ if (mac) {
128
+ return `${ctrl ? '⌃' : ''}${p.alt ? '⌥' : ''}${p.shift ? '⇧' : ''}${meta ? '⌘' : ''}${label}`;
129
+ }
130
+ const parts = [];
131
+ if (ctrl)
132
+ parts.push('Ctrl');
133
+ if (p.alt)
134
+ parts.push('Alt');
135
+ if (p.shift)
136
+ parts.push('Shift');
137
+ if (meta)
138
+ parts.push('Win');
139
+ parts.push(label);
140
+ return parts.join('+');
141
+ }
142
+ export function ariaKeyshortcuts(spec, { mac }) {
143
+ const p = parseShortcut(spec);
144
+ if (!p)
145
+ return '';
146
+ const { ctrl, meta } = resolved(p, mac);
147
+ const key = eventKeyOf(p.key);
148
+ const parts = [];
149
+ if (ctrl)
150
+ parts.push('Control');
151
+ if (p.alt)
152
+ parts.push('Alt');
153
+ if (p.shift)
154
+ parts.push('Shift');
155
+ if (meta)
156
+ parts.push('Meta');
157
+ parts.push(ARIA_KEY[key] ?? (key.length === 1 ? key.toUpperCase() : key));
158
+ return parts.join('+');
159
+ }
160
+ export function matchShortcut(e, spec, { mac }) {
161
+ const p = typeof spec === 'string' ? parseShortcut(spec) : spec;
162
+ if (!p)
163
+ return false;
164
+ const { ctrl, meta } = resolved(p, mac);
165
+ if (e.ctrlKey !== ctrl || e.metaKey !== meta || e.altKey !== p.alt || e.shiftKey !== p.shift)
166
+ return false;
167
+ const token = p.key;
168
+ if (/^[a-z]$/.test(token))
169
+ return e.code === `Key${token.toUpperCase()}`;
170
+ if (/^[0-9]$/.test(token))
171
+ return e.code === `Digit${token}` || e.code === `Numpad${token}`;
172
+ const punctuation = PUNCTUATION_CODE[token];
173
+ if (punctuation)
174
+ return e.code === punctuation;
175
+ return e.key.toLowerCase() === eventKeyOf(token).toLowerCase();
176
+ }