@nakafa/aksara-contracts 3.0.0 → 4.0.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.
@@ -1,5 +1,4 @@
1
1
  import { BigDecimal, Option, Schema } from "effect";
2
- import type { AppLocaleCode } from "#contracts/locale";
3
2
  /**
4
3
  * One exact decimal in its only canonical spelling: an optional minus sign, an
5
4
  * integer part without leading zeros, and a dot-separated fraction without
@@ -22,7 +21,7 @@ export declare const ToleranceSchema: Schema.Struct<{
22
21
  export type Tolerance = typeof ToleranceSchema.Type;
23
22
  /**
24
23
  * Deterministic numeric answer key. A learner number, read by
25
- * `readNumberAnswer` in the delivery language, matches when its exact value
24
+ * `readNumberAnswer`, matches when its exact value
26
25
  * equals `value`, or lies within `tolerance` when present. A fraction such as
27
26
  * `6/8` matches only when `acceptsFractions` is true.
28
27
  */
@@ -87,15 +86,19 @@ export declare function isBlankAnswer(answer: string): boolean;
87
86
  /**
88
87
  * Reads one typed learner number exactly, under the single grammar every
89
88
  * runtime grades with. The input is first read as visible text, as for text
90
- * answers. A decimal is an optional sign (`+`, `-`, or the minus sign U+2212)
91
- * and ASCII digits with at most one decimal separator, a comma for `id` and
92
- * `de` and a dot for `en`, and at least one digit, so `.5` and `5.` read in
93
- * English. A fraction is an optionally signed integer numerator, a slash with
94
- * optional spaces on each side, and a nonzero integer denominator. Grouping
95
- * separators, exponents, mixed numbers such as `1 1/2`, and empty input are
96
- * not numbers.
89
+ * answers, so a right-to-left mark around an Arabic number is no character.
90
+ * Digits are ASCII, Arabic-Indic (`٠` to `٩`), or Eastern Arabic-Indic (`۰` to
91
+ * `۹`), in any mix. A decimal is an optional sign (`+`, `-`, or the minus sign
92
+ * U+2212) and digits with at most one decimal separator and at least one
93
+ * digit, so `.5` and `5.` read. The separator is a dot, a comma, or the Arabic
94
+ * decimal separator `٫` in every language, because a school's paper names no
95
+ * language and a learner's decimal point is never wrong: `0.75`, `0,75`, and
96
+ * `٠٫٧٥` read the same. A fraction is an optionally signed integer numerator,
97
+ * a slash with optional spaces on each side, and a nonzero integer
98
+ * denominator. Grouping separators, a second separator, exponents, mixed
99
+ * numbers such as `1 1/2`, and empty input are not numbers.
97
100
  */
98
- export declare function readNumberAnswer(input: string, language: AppLocaleCode): Option.Option<{
101
+ export declare function readNumberAnswer(input: string): Option.Option<{
99
102
  readonly denominator: BigDecimal.BigDecimal;
100
103
  readonly fraction: boolean;
101
104
  readonly numerator: BigDecimal.BigDecimal;
@@ -103,9 +106,8 @@ export declare function readNumberAnswer(input: string, language: AppLocaleCode)
103
106
  /**
104
107
  * Grades one typed learner answer against its key, as every runtime must. A
105
108
  * text key matches when the answer normalizes to one accepted answer. A
106
- * numeric key reads the answer with `readNumberAnswer` in the delivery
107
- * language and compares its exact value, so an unreadable or empty answer
108
- * never matches.
109
+ * numeric key reads the answer with `readNumberAnswer` and compares its exact
110
+ * value, so an unreadable or empty answer never matches.
109
111
  */
110
- export declare function matchesAnswerKey(key: AnswerKey, answer: string, language: AppLocaleCode): boolean;
112
+ export declare function matchesAnswerKey(key: AnswerKey, answer: string): boolean;
111
113
  export {};
@@ -4,16 +4,13 @@ const UNTYPABLE_CHARACTER_PATTERN = /[\p{Cc}\p{Cf}]|[^\S ]/u;
4
4
  const FORMAT_CHARACTER_PATTERN = /\p{Cf}/gu;
5
5
  const WHITESPACE_PATTERN = /\s/gu;
6
6
  const SPACE_RUN_PATTERN = / {2,}/gu;
7
- const COMMA_NUMBER_PATTERN = /^[+\u2212-]?(?:\d+(?:,\d*)?|,\d+)$/u;
8
- const DOT_NUMBER_PATTERN = /^[+\u2212-]?(?:\d+(?:\.\d*)?|\.\d+)$/u;
7
+ /** Arabic-Indic digits U+0660 to U+0669 and Eastern Arabic-Indic digits U+06F0 to U+06F9. */
8
+ const ARABIC_DIGIT_PATTERN = /[\u0660-\u0669\u06f0-\u06f9]/gu;
9
+ /** The comma and the Arabic decimal separator U+066B, which a decimal may use for the dot. */
10
+ const DECIMAL_SEPARATOR_PATTERN = /[,\u066b]/u;
11
+ const NUMBER_PATTERN = /^[+\u2212-]?(?:\d+(?:[.,\u066b]\d*)?|[.,\u066b]\d+)$/u;
9
12
  const FRACTION_PATTERN = /^[+\u2212-]?\d+ *\/ *\d+$/u;
10
13
  const ONE = BigDecimal.fromBigInt(1n);
11
- /** Typed decimal notation of each delivery language. */
12
- const NUMBER_PATTERNS = {
13
- de: COMMA_NUMBER_PATTERN,
14
- en: DOT_NUMBER_PATTERN,
15
- id: COMMA_NUMBER_PATTERN,
16
- };
17
14
  /**
18
15
  * One exact decimal in its only canonical spelling: an optional minus sign, an
19
16
  * integer part without leading zeros, and a dot-separated fraction without
@@ -44,7 +41,7 @@ export const ToleranceSchema = Schema.Struct({
44
41
  }).check(Schema.makeFilter(({ kind, value }) => kind === "absolute" || value.startsWith("0."), { message: "Expected a relative tolerance below one." }));
45
42
  /**
46
43
  * Deterministic numeric answer key. A learner number, read by
47
- * `readNumberAnswer` in the delivery language, matches when its exact value
44
+ * `readNumberAnswer`, matches when its exact value
48
45
  * equals `value`, or lies within `tolerance` when present. A fraction such as
49
46
  * `6/8` matches only when `acceptsFractions` is true.
50
47
  */
@@ -131,12 +128,21 @@ const NumberAnswerSchema = Schema.Struct({
131
128
  function readSigned(text) {
132
129
  return BigDecimal.fromString(text.trim().replace("\u2212", "-"));
133
130
  }
134
- /** Reads one typed decimal in the notation of its delivery language. */
135
- function readDecimal(typed, language) {
136
- if (!NUMBER_PATTERNS[language].test(typed)) {
131
+ /**
132
+ * Writes each Arabic-Indic or Eastern Arabic-Indic digit as its ASCII digit.
133
+ * Both runs start at a code point whose last hexadecimal digit is zero, so a
134
+ * digit's value is its code point modulo 16.
135
+ */
136
+ function asciiDigits(text) {
137
+ return text.replace(ARABIC_DIGIT_PATTERN, (digit) => String(digit.charCodeAt(0) % 16));
138
+ }
139
+ /** Reads one typed decimal whose separator is a dot, a comma, or U+066B. */
140
+ function readDecimal(typed) {
141
+ if (!NUMBER_PATTERN.test(typed)) {
137
142
  return Option.none();
138
143
  }
139
- return Option.map(readSigned(typed.replace(",", ".")), (numerator) => ({
144
+ const dotted = typed.replace(DECIMAL_SEPARATOR_PATTERN, ".");
145
+ return Option.map(readSigned(dotted), (numerator) => ({
140
146
  denominator: ONE,
141
147
  fraction: false,
142
148
  numerator,
@@ -169,17 +175,21 @@ export function isBlankAnswer(answer) {
169
175
  /**
170
176
  * Reads one typed learner number exactly, under the single grammar every
171
177
  * runtime grades with. The input is first read as visible text, as for text
172
- * answers. A decimal is an optional sign (`+`, `-`, or the minus sign U+2212)
173
- * and ASCII digits with at most one decimal separator, a comma for `id` and
174
- * `de` and a dot for `en`, and at least one digit, so `.5` and `5.` read in
175
- * English. A fraction is an optionally signed integer numerator, a slash with
176
- * optional spaces on each side, and a nonzero integer denominator. Grouping
177
- * separators, exponents, mixed numbers such as `1 1/2`, and empty input are
178
- * not numbers.
178
+ * answers, so a right-to-left mark around an Arabic number is no character.
179
+ * Digits are ASCII, Arabic-Indic (`٠` to `٩`), or Eastern Arabic-Indic (`۰` to
180
+ * `۹`), in any mix. A decimal is an optional sign (`+`, `-`, or the minus sign
181
+ * U+2212) and digits with at most one decimal separator and at least one
182
+ * digit, so `.5` and `5.` read. The separator is a dot, a comma, or the Arabic
183
+ * decimal separator `٫` in every language, because a school's paper names no
184
+ * language and a learner's decimal point is never wrong: `0.75`, `0,75`, and
185
+ * `٠٫٧٥` read the same. A fraction is an optionally signed integer numerator,
186
+ * a slash with optional spaces on each side, and a nonzero integer
187
+ * denominator. Grouping separators, a second separator, exponents, mixed
188
+ * numbers such as `1 1/2`, and empty input are not numbers.
179
189
  */
180
- export function readNumberAnswer(input, language) {
181
- const typed = visibleText(input);
182
- return Option.orElse(readDecimal(typed, language), () => readFraction(typed));
190
+ export function readNumberAnswer(input) {
191
+ const typed = asciiDigits(visibleText(input));
192
+ return Option.orElse(readDecimal(typed), () => readFraction(typed));
183
193
  }
184
194
  /**
185
195
  * Compares one exact learner number with a numeric key. For an answer
@@ -203,14 +213,13 @@ function matchesNumberKey(key, answer) {
203
213
  /**
204
214
  * Grades one typed learner answer against its key, as every runtime must. A
205
215
  * text key matches when the answer normalizes to one accepted answer. A
206
- * numeric key reads the answer with `readNumberAnswer` in the delivery
207
- * language and compares its exact value, so an unreadable or empty answer
208
- * never matches.
216
+ * numeric key reads the answer with `readNumberAnswer` and compares its exact
217
+ * value, so an unreadable or empty answer never matches.
209
218
  */
210
- export function matchesAnswerKey(key, answer, language) {
219
+ export function matchesAnswerKey(key, answer) {
211
220
  if (key.kind === "text") {
212
221
  const typed = normalizeTextAnswer(answer, key);
213
222
  return Arr.some(key.acceptedAnswers, (accepted) => normalizeTextAnswer(accepted, key) === typed);
214
223
  }
215
- return Option.exists(readNumberAnswer(answer, language), (number) => matchesNumberKey(key, number));
224
+ return Option.exists(readNumberAnswer(answer), (number) => matchesNumberKey(key, number));
216
225
  }
package/dist/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DateTime, Schema } from "effect";
2
- const DATE_ONLY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/u;
2
+ const DATE_ONLY_PATTERN = /^\d{4}-\d{2}-\d{2}$/u;
3
3
  /** Checks a source-authored date against the real ISO calendar. */
4
4
  function isDateOnly(value) {
5
5
  if (!DATE_ONLY_PATTERN.test(value)) {
@@ -54,7 +54,7 @@ const QuranTranslationSourceSchema = Schema.Struct({
54
54
  /** Reads every numeric marker without treating editorial brackets as notes. */
55
55
  function readMarkers(source) {
56
56
  const markers = MutableList.make();
57
- for (const match of source.matchAll(/\[(\d+)\]/gu)) {
57
+ for (const match of source.matchAll(/\[\d+\]/gu)) {
58
58
  const rawNumber = match[0].slice(1, -1);
59
59
  const start = match.index;
60
60
  const number = Number(rawNumber);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakafa/aksara-contracts",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Schemas and verification contracts for Nakafa content releases.",
5
5
  "homepage": "https://github.com/nakafaai/aksara#readme",
6
6
  "license": "SEE LICENSE IN LICENSE",