@pie-element/shared-controller-utils 0.1.5-next.20261023101922 → 0.1.5-next.20261024053312

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/README.md CHANGED
@@ -20,6 +20,21 @@ This package is part of the PIE workspace and should be installed via workspace
20
20
  import { getShuffledChoices, lockChoices, partialScoring } from '@pie-element/shared-controller-utils';
21
21
  ```
22
22
 
23
+ ### Response normalization
24
+
25
+ `normalizeResponse` folds a typed response and an authored answer to one comparable form before a controller compares them: bidi controls and tatweel removed, Arabic presentation forms folded, NFC, every Unicode decimal digit folded to 0–9, and `٫` and `٬` read as the content language's decimal and group marks. An ASCII string passes through unchanged, so existing answers score as before. The session keeps the response as entered.
26
+
27
+ ```typescript
28
+ import { contentLanguage, normalizeResponse } from '@pie-element/shared-controller-utils';
29
+
30
+ const language = contentLanguage(model, env); // model.language, then env.locale
31
+ const matches =
32
+ normalizeResponse(typed, { language, ignoreCase: true }) ===
33
+ normalizeResponse(authored, { language, ignoreCase: true });
34
+ ```
35
+
36
+ `ignoreCase` folds with `toLocaleLowerCase` in Turkish and Azerbaijani, so İ/ı fold correctly, and with `toLowerCase()` in every other language. A tag the runtime has no locale data for keeps `.` and `,`. `foldLetterVariants` folds Arabic hamza and madda alef forms, tashkeel, ة and ى; it changes what counts as correct spelling, so it stays an authoring choice per response. Math controllers call `normalizeResponse` without a language, so `٫` becomes the `.` math-validation computes on.
37
+
23
38
  ## Development
24
39
 
25
40
  ```bash
package/dist/index.d.ts CHANGED
@@ -6,3 +6,5 @@
6
6
  */
7
7
  export { lockChoices, getShuffledChoices } from './persistence.js';
8
8
  export * as partialScoring from './partial-scoring.js';
9
+ export { contentLanguage, foldCase, foldDigits, normalizeResponse } from './response-normalizer.js';
10
+ export type { NormalizeOptions } from './response-normalizer.js';
package/dist/index.js CHANGED
@@ -50,4 +50,45 @@ function c(e, t, n) {
50
50
  return e?.partialScoring === !1 || t?.partialScoring === !1 ? !1 : typeof n != "boolean" || n;
51
51
  }
52
52
  //#endregion
53
- export { a as getShuffledChoices, o as lockChoices, s as partialScoring };
53
+ //#region src/response-normalizer.ts
54
+ var l = /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069\u0640]/g, u = /[\uFB50-\uFDFF\uFE70-\uFEFC]+/g, d = /\p{Nd}/u, f = /(?![0-9])\p{Nd}/gu, p = /\u066B/g, m = /\u066C/g, h = /[\u0622\u0623\u0625\u0671]/g, g = /[\u064B-\u065F\u0670]/g, _ = /* @__PURE__ */ new Map(), v = (e) => {
55
+ let t = _.get(e);
56
+ if (t !== void 0) return t;
57
+ let n = 0;
58
+ for (; d.test(String.fromCodePoint(e - n - 1));) n++;
59
+ let r = n % 10;
60
+ return _.set(e, r), r;
61
+ }, y = (e) => e.replace(f, (e) => String(v(e.codePointAt(0)))), b = {
62
+ decimal: ".",
63
+ group: ","
64
+ }, x = /* @__PURE__ */ new Map(), S = (e) => {
65
+ if (!e) return b;
66
+ let t = x.get(e);
67
+ if (t) return t;
68
+ let n = b;
69
+ try {
70
+ if (Intl.NumberFormat.supportedLocalesOf(e).length > 0) {
71
+ let t = new Intl.NumberFormat(e, { numberingSystem: "latn" }).formatToParts(12345.6);
72
+ n = {
73
+ decimal: t.find((e) => e.type === "decimal")?.value ?? ".",
74
+ group: t.find((e) => e.type === "group")?.value ?? ","
75
+ };
76
+ }
77
+ } catch {}
78
+ return x.set(e, n), n;
79
+ }, C = (e, t) => {
80
+ let n = [e?.language, t?.locale];
81
+ for (let e of n) if (typeof e == "string" && e.trim()) return e.trim().replace(/_/g, "-");
82
+ }, w = /^(tr|az)(-|$)/i, T = (e, t) => {
83
+ if (!t || !w.test(t)) return e.toLowerCase();
84
+ try {
85
+ return e.toLocaleLowerCase(t);
86
+ } catch {
87
+ return e.toLowerCase();
88
+ }
89
+ }, E = (e, t = {}) => {
90
+ let n = S(t.language), r = (t.ignoreCase ? T(e, t.language) : e).replace(l, "").replace(u, (e) => e.normalize("NFKC")).normalize("NFC");
91
+ return r = y(r).replace(p, n.decimal).replace(m, n.group), t.foldLetterVariants && (r = r.replace(g, "").replace(h, "ا").replace(/\u0629/g, "ه").replace(/\u0649/g, "ي")), r;
92
+ };
93
+ //#endregion
94
+ export { C as contentLanguage, T as foldCase, y as foldDigits, a as getShuffledChoices, o as lockChoices, E as normalizeResponse, s as partialScoring };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Response Normalizer
3
+ *
4
+ * Folds a typed response and an authored answer to one comparable form, so that
5
+ * the same answer typed with other digits, Arabic separators or a different
6
+ * Unicode encoding compares equal. Controllers apply it inside the comparison
7
+ * and store the response as entered.
8
+ *
9
+ * Nothing in an ASCII string is altered unless case folding is asked for, so
10
+ * a response that compared equal or unequal before still does.
11
+ */
12
+ export interface NormalizeOptions {
13
+ /**
14
+ * BCP 47 tag of the content language. Selects the decimal and group marks
15
+ * that the Arabic separators fold to, and the case fold. Without one, the
16
+ * marks are `.` and `,`, the form math.js and math-validation compute on.
17
+ */
18
+ language?: string;
19
+ /** Fold case, with the Turkish and Azerbaijani i when the language is one of them. */
20
+ ignoreCase?: boolean;
21
+ /**
22
+ * Fold Arabic spelling variants: hamza and madda alef forms to bare alef,
23
+ * tashkeel removed, ة to ه and ى to ي. An authoring choice per response,
24
+ * because it changes what counts as correct spelling.
25
+ */
26
+ foldLetterVariants?: boolean;
27
+ }
28
+ /** Every Unicode decimal digit (`\p{Nd}`) replaced by its ASCII digit. */
29
+ export declare const foldDigits: (value: string) => string;
30
+ /**
31
+ * The language a comparison runs in: the element's `model.language`, then the
32
+ * item's `env.locale`. The translator's underscore form (`tr_TR`) becomes a
33
+ * BCP 47 tag.
34
+ */
35
+ export declare const contentLanguage: (model?: object | null, env?: object | null) => string | undefined;
36
+ /**
37
+ * Lower case for comparison: `toLocaleLowerCase` in Turkish and Azerbaijani,
38
+ * so dotted and dotless i fold correctly, and `toLowerCase()` otherwise.
39
+ */
40
+ export declare const foldCase: (value: string, language?: string) => string;
41
+ /**
42
+ * The comparable form of a typed response or authored answer: case folded
43
+ * when the options ask for it; bidi controls and tatweel removed; Arabic
44
+ * presentation forms folded to base letters; NFC; every decimal digit folded
45
+ * to 0–9; `٫` and `٬` replaced by the content language's decimal and group
46
+ * marks. Arabic letter variants fold only when the options ask for it.
47
+ */
48
+ export declare const normalizeResponse: (value: string, options?: NormalizeOptions) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-controller-utils",
3
- "version": "0.1.5-next.20261023101922",
3
+ "version": "0.1.5-next.20261024053312",
4
4
  "description": "Controller utility functions for PIE elements",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,7 +21,7 @@
21
21
  "clean": "rm -rf dist"
22
22
  },
23
23
  "dependencies": {
24
- "@pie-element/shared-types": "0.2.4-next.20261023101922"
24
+ "@pie-element/shared-types": "0.2.4-next.20261024053312"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"