@astl/engine 0.0.0 → 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 (98) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +202 -0
  3. package/README.md +41 -0
  4. package/dist/analyse.d.ts +68 -0
  5. package/dist/analyse.d.ts.map +1 -0
  6. package/dist/analyse.js +1179 -0
  7. package/dist/analyse.js.map +1 -0
  8. package/dist/checker.d.ts +19 -0
  9. package/dist/checker.d.ts.map +1 -0
  10. package/dist/checker.js +103 -0
  11. package/dist/checker.js.map +1 -0
  12. package/dist/compile.d.ts +16 -0
  13. package/dist/compile.d.ts.map +1 -0
  14. package/dist/compile.js +329 -0
  15. package/dist/compile.js.map +1 -0
  16. package/dist/conformance.d.ts +35 -0
  17. package/dist/conformance.d.ts.map +1 -0
  18. package/dist/conformance.js +101 -0
  19. package/dist/conformance.js.map +1 -0
  20. package/dist/engine.d.ts +13 -0
  21. package/dist/engine.d.ts.map +1 -0
  22. package/dist/engine.js +231 -0
  23. package/dist/engine.js.map +1 -0
  24. package/dist/grade.d.ts +9 -0
  25. package/dist/grade.d.ts.map +1 -0
  26. package/dist/grade.js +192 -0
  27. package/dist/grade.js.map +1 -0
  28. package/dist/guard.d.ts +45 -0
  29. package/dist/guard.d.ts.map +1 -0
  30. package/dist/guard.js +327 -0
  31. package/dist/guard.js.map +1 -0
  32. package/dist/help.d.ts +32 -0
  33. package/dist/help.d.ts.map +1 -0
  34. package/dist/help.js +120 -0
  35. package/dist/help.js.map +1 -0
  36. package/dist/html.d.ts +31 -0
  37. package/dist/html.d.ts.map +1 -0
  38. package/dist/html.js +246 -0
  39. package/dist/html.js.map +1 -0
  40. package/dist/ids.d.ts +35 -0
  41. package/dist/ids.d.ts.map +1 -0
  42. package/dist/ids.js +126 -0
  43. package/dist/ids.js.map +1 -0
  44. package/dist/index.d.ts +13 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +10 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/kit/attributes.d.ts +6 -0
  49. package/dist/kit/attributes.d.ts.map +1 -0
  50. package/dist/kit/attributes.js +20 -0
  51. package/dist/kit/attributes.js.map +1 -0
  52. package/dist/kit/index.d.ts +43 -0
  53. package/dist/kit/index.d.ts.map +1 -0
  54. package/dist/kit/index.js +118 -0
  55. package/dist/kit/index.js.map +1 -0
  56. package/dist/limits.d.ts +17 -0
  57. package/dist/limits.d.ts.map +1 -0
  58. package/dist/limits.js +173 -0
  59. package/dist/limits.js.map +1 -0
  60. package/dist/messages/en.json +48 -0
  61. package/dist/messages/pl.json +48 -0
  62. package/dist/messages.d.ts +4 -0
  63. package/dist/messages.d.ts.map +1 -0
  64. package/dist/messages.js +9 -0
  65. package/dist/messages.js.map +1 -0
  66. package/dist/round.d.ts +3 -0
  67. package/dist/round.d.ts.map +1 -0
  68. package/dist/round.js +5 -0
  69. package/dist/round.js.map +1 -0
  70. package/dist/slot-kinds.d.ts +58 -0
  71. package/dist/slot-kinds.d.ts.map +1 -0
  72. package/dist/slot-kinds.js +151 -0
  73. package/dist/slot-kinds.js.map +1 -0
  74. package/dist/text.d.ts +23 -0
  75. package/dist/text.d.ts.map +1 -0
  76. package/dist/text.js +236 -0
  77. package/dist/text.js.map +1 -0
  78. package/package.json +51 -4
  79. package/src/analyse.ts +1396 -0
  80. package/src/checker.ts +132 -0
  81. package/src/compile.ts +394 -0
  82. package/src/conformance.ts +120 -0
  83. package/src/engine.ts +274 -0
  84. package/src/grade.ts +229 -0
  85. package/src/guard.ts +356 -0
  86. package/src/help.ts +141 -0
  87. package/src/html.ts +274 -0
  88. package/src/ids.ts +144 -0
  89. package/src/index.ts +12 -0
  90. package/src/kit/attributes.ts +21 -0
  91. package/src/kit/index.ts +140 -0
  92. package/src/limits.ts +153 -0
  93. package/src/messages/en.json +48 -0
  94. package/src/messages/pl.json +48 -0
  95. package/src/messages.ts +9 -0
  96. package/src/round.ts +4 -0
  97. package/src/slot-kinds.ts +212 -0
  98. package/src/text.ts +234 -0
package/src/text.ts ADDED
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Text comparison and number parsing for grading. Pure functions: a stored key grades the same way on every runtime.
3
+ */
4
+ import type { NormalisationProfile } from '@astl/core';
5
+
6
+ /**
7
+ * A global character class from code points. They stay numbers on purpose: formatters turn \u escapes into the raw
8
+ * characters, and several of these are invisible or look like a plain hyphen.
9
+ */
10
+ function anyOf(...codePoints: number[]): RegExp {
11
+ return new RegExp(`[${String.fromCodePoint(...codePoints)}]`, 'gu');
12
+ }
13
+
14
+ const SINGLE_QUOTES = anyOf(0x2018, 0x2019, 0x201a, 0x201b, 0x2032);
15
+ const DOUBLE_QUOTES = anyOf(0x201c, 0x201d, 0x201e, 0x201f, 0x2033, 0x00ab, 0x00bb);
16
+ const DASHES = anyOf(0x2012, 0x2013, 0x2014, 0x2212);
17
+ const ELLIPSIS = anyOf(0x2026);
18
+ const MINUS_SIGN = anyOf(0x2212);
19
+ const SPACES = /\s+/gu;
20
+ const PUNCTUATION = /^\p{P}$/u;
21
+ const WHITESPACE = /^\s$/u;
22
+ const MARKS = /\p{M}/gu;
23
+
24
+ /** Letters that NFD does not decompose but that `marks: fold` must treat as their base letters. */
25
+ const FOLDED_LETTERS: Readonly<Record<string, string>> = {
26
+ ł: 'l',
27
+ Ł: 'L',
28
+ ø: 'o',
29
+ Ø: 'O',
30
+ đ: 'd',
31
+ Đ: 'D',
32
+ ß: 'ss',
33
+ æ: 'ae',
34
+ Æ: 'AE',
35
+ œ: 'oe',
36
+ Œ: 'OE',
37
+ ı: 'i',
38
+ };
39
+ const FOLDED_LETTER = /[łŁøØđĐßæÆœŒı]/g;
40
+
41
+ /**
42
+ * Normalises typed text for comparison. Steps, each only when the profile asks for it: Unicode form, quote and
43
+ * dash folding, space collapsing, edge punctuation, case (locale-aware, so Turkish İ and I work) and diacritics.
44
+ * A late step can leave work for an earlier one (a dropped combining mark between two spaces, or after a final
45
+ * full stop), so the steps repeat until the text stops changing: the result is always a fixpoint.
46
+ */
47
+ export function normalise(text: string, profile: NormalisationProfile, lang: string): string {
48
+ let s = text;
49
+ for (let pass = 0; pass < MAX_PASSES; pass++) {
50
+ const next = normaliseOnce(s, profile, lang);
51
+ if (next === s) break;
52
+ s = next;
53
+ }
54
+ return s;
55
+ }
56
+
57
+ /** Real text settles after one or two passes; the cap only bounds pathological input. */
58
+ const MAX_PASSES = 8;
59
+
60
+ function normaliseOnce(text: string, profile: NormalisationProfile, lang: string): string {
61
+ let s = text.normalize(profile.form);
62
+ if (profile.quotes === 'fold') {
63
+ s = s
64
+ .replace(SINGLE_QUOTES, "'")
65
+ .replace(DOUBLE_QUOTES, '"')
66
+ .replace(DASHES, '-')
67
+ .replace(ELLIPSIS, '...');
68
+ }
69
+ const collapse = profile.space === 'collapse';
70
+ if (collapse) s = s.replace(SPACES, ' ').trim();
71
+ if (profile.punctuation === 'ignore-edge') s = stripEdgePunctuation(s, collapse);
72
+ if (profile.case === 'insensitive') s = lowerCase(s, lang);
73
+ if (profile.marks === 'fold') {
74
+ s = s
75
+ .normalize('NFD')
76
+ .replace(MARKS, '')
77
+ .replace(FOLDED_LETTER, (ch) => FOLDED_LETTERS[ch] ?? ch)
78
+ .normalize('NFC');
79
+ }
80
+ return s;
81
+ }
82
+
83
+ /**
84
+ * Drops punctuation at both ends (and, when trimming, the spaces it uncovers) until nothing changes. An answer made
85
+ * only of punctuation (`?`) is kept. One scan from each end: `/\p{P}+$/` would be quadratic on `a!!!...!a`.
86
+ */
87
+ function stripEdgePunctuation(value: string, trim: boolean): string {
88
+ const strip = (codePoint: number): boolean => {
89
+ const ch = String.fromCodePoint(codePoint);
90
+ return PUNCTUATION.test(ch) || (trim && WHITESPACE.test(ch));
91
+ };
92
+ let start = 0;
93
+ let end = value.length;
94
+ while (start < end) {
95
+ const codePoint = value.codePointAt(start) ?? 0;
96
+ if (!strip(codePoint)) break;
97
+ start += codePoint > 0xffff ? 2 : 1;
98
+ }
99
+ while (end > start) {
100
+ let last = end - 1;
101
+ const unit = value.charCodeAt(last);
102
+ if (unit >= 0xdc00 && unit <= 0xdfff && last > start) {
103
+ const high = value.charCodeAt(last - 1);
104
+ if (high >= 0xd800 && high <= 0xdbff) last--;
105
+ }
106
+ if (!strip(value.codePointAt(last) ?? 0)) break;
107
+ end = last;
108
+ }
109
+ if (start === end) return value;
110
+ return start === 0 && end === value.length ? value : value.slice(start, end);
111
+ }
112
+
113
+ function lowerCase(value: string, lang: string): string {
114
+ try {
115
+ return value.toLocaleLowerCase(lang);
116
+ } catch {
117
+ // A malformed language tag in a stored key must not break grading.
118
+ return value.toLowerCase();
119
+ }
120
+ }
121
+
122
+ /** Languages that write a decimal comma (`0,5`), by primary subtag. */
123
+ const COMMA_DECIMAL: ReadonlySet<string> = new Set([
124
+ 'pl',
125
+ 'de',
126
+ 'fr',
127
+ 'es',
128
+ 'it',
129
+ 'pt',
130
+ 'ru',
131
+ 'uk',
132
+ 'cs',
133
+ 'sk',
134
+ 'sl',
135
+ 'hr',
136
+ 'sr',
137
+ 'bg',
138
+ 'ro',
139
+ 'hu',
140
+ 'lt',
141
+ 'lv',
142
+ 'et',
143
+ 'fi',
144
+ 'sv',
145
+ 'nb',
146
+ 'nn',
147
+ 'da',
148
+ 'nl',
149
+ 'tr',
150
+ 'el',
151
+ 'id',
152
+ 'vi',
153
+ 'be',
154
+ 'kk',
155
+ 'az',
156
+ ]);
157
+
158
+ /** True when the content language writes a decimal comma. */
159
+ export function usesDecimalComma(lang: string): boolean {
160
+ return COMMA_DECIMAL.has(lang.toLowerCase().split('-')[0] ?? '');
161
+ }
162
+
163
+ /** Decimal-comma languages that group thousands with a dot (`1.234`), by primary subtag (CLDR). */
164
+ const DOT_GROUPING: ReadonlySet<string> = new Set([
165
+ 'de',
166
+ 'es',
167
+ 'it',
168
+ 'pt',
169
+ 'sl',
170
+ 'hr',
171
+ 'sr',
172
+ 'ro',
173
+ 'da',
174
+ 'nl',
175
+ 'tr',
176
+ 'el',
177
+ 'id',
178
+ 'vi',
179
+ 'az',
180
+ ]);
181
+
182
+ const PLAIN_NUMBER = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
183
+ const COMMA_GROUPED = /^[+-]?\d{1,3}(,\d{3})+(\.\d*)?$/;
184
+ const DOT_GROUPED = /^[+-]?\d{1,3}(\.\d{3})+(,\d*)?$/;
185
+ const DOT_GROUPED_INTEGER = /^[+-]?\d{1,3}(\.\d{3})+$/;
186
+
187
+ /**
188
+ * Parses a number as a student types it in the content language. Spaces may group digits; U+2212 is a minus sign.
189
+ * Decimal-comma languages accept `0,5`, `1.234,5` and a dot as the only separator: `1.234.567` is grouped, and a
190
+ * single `1.234` is grouped where the language groups with dots (German) but a decimal elsewhere (Polish). Other
191
+ * languages accept `0.5` and `1,234.5`. Returns null for anything else.
192
+ */
193
+ export function parseNumber(text: string, lang: string): number | null {
194
+ let s = text
195
+ .trim()
196
+ .replace(/(\d)\s+(?=\d)/gu, '$1')
197
+ .replace(MINUS_SIGN, '-');
198
+ const commas = count(s, ',');
199
+ const dots = count(s, '.');
200
+ const primary = lang.toLowerCase().split('-')[0] ?? '';
201
+ if (COMMA_DECIMAL.has(primary)) {
202
+ if (commas > 0 && dots > 0) {
203
+ if (commas > 1 || !DOT_GROUPED.test(s)) return null;
204
+ s = s.replace(/\./g, '').replace(',', '.');
205
+ } else if (commas === 1) {
206
+ s = s.replace(',', '.');
207
+ } else if (commas > 1) {
208
+ return null;
209
+ } else if (dots > 0 && DOT_GROUPED_INTEGER.test(s) && (dots > 1 || DOT_GROUPING.has(primary))) {
210
+ s = s.replace(/\./g, '');
211
+ } else if (dots > 1) {
212
+ return null;
213
+ }
214
+ } else if (commas > 0) {
215
+ if (!COMMA_GROUPED.test(s)) return null;
216
+ s = s.replace(/,/g, '');
217
+ }
218
+ if (!PLAIN_NUMBER.test(s)) return null;
219
+ const value = Number(s);
220
+ return Number.isFinite(value) ? value : null;
221
+ }
222
+
223
+ function count(value: string, ch: string): number {
224
+ let n = 0;
225
+ for (const c of value) if (c === ch) n++;
226
+ return n;
227
+ }
228
+
229
+ /** Formats a number for display in the content language: no grouping, decimal comma where the language uses one. */
230
+ export function formatNumber(value: number, lang: string): string {
231
+ let s = String(value);
232
+ if (/e/i.test(s) && Math.abs(value) < 1e21) s = value.toFixed(20).replace(/\.?0+$/, '');
233
+ return usesDecimalComma(lang) ? s.replace('.', ',') : s;
234
+ }