@qti-components/delivery 0.1.0-next.2 → 0.1.0-next.3

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/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './model/test-model.js';
2
2
  export * from './model/item-facts.js';
3
3
  export type { XmlElement, XmlNode } from './model/xml-node.js';
4
4
  export * from './random/seeded-random.js';
5
+ export * from './random/selection.js';
5
6
  export * from './random/shuffle.js';
6
7
  export * from './state/clock.js';
7
8
  export * from './state/commands.js';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './model/test-model.js';
2
2
  export * from './model/item-facts.js';
3
3
  export * from './random/seeded-random.js';
4
+ export * from './random/selection.js';
4
5
  export * from './random/shuffle.js';
5
6
  export * from './state/clock.js';
6
7
  export * from './state/commands.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './model/test-model.js';\nexport * from './model/item-facts.js';\nexport type { XmlElement, XmlNode } from './model/xml-node.js';\nexport * from './random/seeded-random.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './model/test-model.js';\nexport * from './model/item-facts.js';\nexport type { XmlElement, XmlNode } from './model/xml-node.js';\nexport * from './random/seeded-random.js';\nexport * from './random/selection.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
@@ -0,0 +1,36 @@
1
+ import type { RandomSource } from './shuffle.js';
2
+ /** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */
3
+ export interface SelectionUnit {
4
+ /** `required="true"`: always in the selection. */
5
+ required: boolean;
6
+ }
7
+ /** A section's `qti-selection` (QTI 3 info model §5.129). */
8
+ export interface SelectionRule {
9
+ /** How many units to select. */
10
+ select: number;
11
+ /** Whether a unit may be selected more than once (its clones). Defaults to false. */
12
+ withReplacement?: boolean;
13
+ }
14
+ /**
15
+ * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
16
+ * unit selected more than once (`with-replacement`) has its index repeated, the clones right after
17
+ * it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
18
+ * the original order").
19
+ *
20
+ * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
21
+ * selects the same units for the same seed:
22
+ *
23
+ * 1. Every required unit is in.
24
+ * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
25
+ * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
26
+ * `i` from 0 while fewer are picked than needed, swapping position `i` with
27
+ * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
28
+ * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
29
+ * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
30
+ * often it was cloned before.
31
+ *
32
+ * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
33
+ * more, below the number of required units, or above the number of units without replacement (or
34
+ * above zero with no units at all).
35
+ */
36
+ export declare function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[];
@@ -0,0 +1,52 @@
1
+ const randomValue = (source) => (typeof source === 'function' ? source() : source.random());
2
+ /**
3
+ * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
4
+ * unit selected more than once (`with-replacement`) has its index repeated, the clones right after
5
+ * it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
6
+ * the original order").
7
+ *
8
+ * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
9
+ * selects the same units for the same seed:
10
+ *
11
+ * 1. Every required unit is in.
12
+ * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
13
+ * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
14
+ * `i` from 0 while fewer are picked than needed, swapping position `i` with
15
+ * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
16
+ * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
17
+ * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
18
+ * often it was cloned before.
19
+ *
20
+ * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
21
+ * more, below the number of required units, or above the number of units without replacement (or
22
+ * above zero with no units at all).
23
+ */
24
+ export function selectUnits(units, rule, random) {
25
+ const { select, withReplacement = false } = rule;
26
+ if (!Number.isInteger(select) || select < 0) {
27
+ throw new RangeError(`qti-selection select="${select}" is not a whole number of zero or more.`);
28
+ }
29
+ const required = units.flatMap((unit, index) => (unit.required ? [index] : []));
30
+ if (select < required.length) {
31
+ throw new RangeError(`qti-selection select="${select}" is less than the ${required.length} required items and subsections.`);
32
+ }
33
+ if (select > units.length && (!withReplacement || units.length === 0)) {
34
+ throw new RangeError(`qti-selection select="${select}" is more than the ${units.length} items and subsections, without with-replacement="true".`);
35
+ }
36
+ const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));
37
+ const needed = Math.min(select, units.length) - required.length;
38
+ if (needed < optional.length) {
39
+ for (let i = 0; i < needed; i++) {
40
+ const j = i + Math.floor(randomValue(random) * (optional.length - i));
41
+ [optional[i], optional[j]] = [optional[j], optional[i]];
42
+ }
43
+ }
44
+ const occurrences = new Array(units.length).fill(0);
45
+ for (const index of [...required, ...optional.slice(0, needed)])
46
+ occurrences[index] = 1;
47
+ for (let clone = units.length; clone < select; clone++) {
48
+ occurrences[Math.floor(randomValue(random) * units.length)]++;
49
+ }
50
+ return occurrences.flatMap((count, index) => new Array(count).fill(index));
51
+ }
52
+ //# sourceMappingURL=selection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selection.js","sourceRoot":"","sources":["../../src/random/selection.ts"],"names":[],"mappings":"AAgBA,MAAM,WAAW,GAAG,CAAC,MAAoB,EAAU,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAElH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,WAAW,CAAC,KAA+B,EAAE,IAAmB,EAAE,MAAoB;IACpG,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;IACjD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,UAAU,CAAC,yBAAyB,MAAM,0CAA0C,CAAC,CAAC;IAClG,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,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AAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5D,KAAK,MAAM,KAAK,IAAI,CAAC,GAAG,QAAQ,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAAE,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAExF,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAChE,CAAC;IAED,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAS,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACrF,CAAC","sourcesContent":["import type { RandomSource } from './shuffle.js';\n\n/** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */\nexport interface SelectionUnit {\n /** `required=\"true\"`: always in the selection. */\n required: boolean;\n}\n\n/** A section's `qti-selection` (QTI 3 info model §5.129). */\nexport interface SelectionRule {\n /** How many units to select. */\n select: number;\n /** Whether a unit may be selected more than once (its clones). Defaults to false. */\n withReplacement?: boolean;\n}\n\nconst randomValue = (source: RandomSource): number => (typeof source === 'function' ? source() : source.random());\n\n/**\n * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a\n * unit selected more than once (`with-replacement`) has its index repeated, the clones right after\n * it. Ordering is a later, separate step (implementation guide §4.5.1: selection \"should preserve\n * the original order\").\n *\n * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)\n * selects the same units for the same seed:\n *\n * 1. Every required unit is in.\n * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with\n * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for\n * `i` from 0 while fewer are picked than needed, swapping position `i` with\n * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.\n * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further\n * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however\n * often it was cloned before.\n *\n * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or\n * more, below the number of required units, or above the number of units without replacement (or\n * above zero with no units at all).\n */\nexport function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[] {\n const { select, withReplacement = false } = rule;\n if (!Number.isInteger(select) || select < 0) {\n throw new RangeError(`qti-selection select=\"${select}\" is not a whole number of zero or more.`);\n }\n const required = units.flatMap((unit, index) => (unit.required ? [index] : []));\n if (select < required.length) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is less than the ${required.length} required items and subsections.`\n );\n }\n if (select > units.length && (!withReplacement || units.length === 0)) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is more than the ${units.length} items and subsections, without with-replacement=\"true\".`\n );\n }\n\n const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));\n const needed = Math.min(select, units.length) - required.length;\n if (needed < optional.length) {\n for (let i = 0; i < needed; i++) {\n const j = i + Math.floor(randomValue(random) * (optional.length - i));\n [optional[i], optional[j]] = [optional[j], optional[i]];\n }\n }\n const occurrences = new Array<number>(units.length).fill(0);\n for (const index of [...required, ...optional.slice(0, needed)]) occurrences[index] = 1;\n\n for (let clone = units.length; clone < select; clone++) {\n occurrences[Math.floor(randomValue(random) * units.length)]++;\n }\n\n return occurrences.flatMap((count, index) => new Array<number>(count).fill(index));\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qti-components/delivery",
3
3
  "description": "The QTI 3 delivery engine: test model, item session lifecycle, sequencing and session state, with no browser globals",
4
- "version": "0.1.0-next.2",
4
+ "version": "0.1.0-next.3",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",