@nakafa/aksara-contracts 3.0.1 → 4.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/dist/assessment/answer.d.ts +16 -14
- package/dist/assessment/answer.js +37 -28
- package/package.json +2 -2
|
@@ -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
|
|
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
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
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
|
|
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`
|
|
107
|
-
*
|
|
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
|
|
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
|
-
|
|
8
|
-
const
|
|
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
|
|
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
|
-
/**
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
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
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
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
|
|
181
|
-
const typed = visibleText(input);
|
|
182
|
-
return Option.orElse(readDecimal(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`
|
|
207
|
-
*
|
|
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
|
|
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
|
|
224
|
+
return Option.exists(readNumberAnswer(answer), (number) => matchesNumberKey(key, number));
|
|
216
225
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nakafa/aksara-contracts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.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",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
|
-
"node": ">=24.0.0 <
|
|
9
|
+
"node": ">=24.0.0 <27.0.0"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public",
|