@pie-element/shared-controller-utils 0.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/README.md +41 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/partial-scoring.d.ts +27 -0
- package/dist/partial-scoring.d.ts.map +1 -0
- package/dist/persistence.d.ts +64 -0
- package/dist/persistence.d.ts.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @pie-framework/controller-utils
|
|
2
|
+
|
|
3
|
+
Controller utility functions for PIE elements.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
This package is part of the PIE workspace and should be installed via workspace dependencies:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@pie-framework/controller-utils": "workspace:*"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { /* utility functions */ } from '@pie-framework/controller-utils';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Development
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# Build the package
|
|
27
|
+
npm run build
|
|
28
|
+
|
|
29
|
+
# Watch mode for development
|
|
30
|
+
npm run dev
|
|
31
|
+
|
|
32
|
+
# Run tests
|
|
33
|
+
npm run test
|
|
34
|
+
|
|
35
|
+
# Clean build artifacts
|
|
36
|
+
npm run clean
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controller Utilities
|
|
3
|
+
*
|
|
4
|
+
* Shared utilities for PIE element controllers, including partial scoring
|
|
5
|
+
* and choice persistence/shuffling.
|
|
6
|
+
*/
|
|
7
|
+
export { lockChoices, getShuffledChoices } from './persistence';
|
|
8
|
+
export * as partialScoring from './partial-scoring';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,KAAK,cAAc,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function u(r) {
|
|
2
|
+
return Array.isArray(r) ? r.filter((e) => e != null) : [];
|
|
3
|
+
}
|
|
4
|
+
function c(r) {
|
|
5
|
+
const e = [...r];
|
|
6
|
+
for (let t = e.length - 1; t > 0; t--) {
|
|
7
|
+
const n = Math.floor(Math.random() * (t + 1));
|
|
8
|
+
[e[t], e[n]] = [e[n], e[t]];
|
|
9
|
+
}
|
|
10
|
+
return e;
|
|
11
|
+
}
|
|
12
|
+
function a(r) {
|
|
13
|
+
return r == null ? !0 : Array.isArray(r) ? r.length === 0 : typeof r == "object" ? Object.keys(r).length === 0 : !1;
|
|
14
|
+
}
|
|
15
|
+
async function h(r, e, t, n = "value") {
|
|
16
|
+
const i = u(e?.data?.shuffledValues ?? e?.shuffledValues ?? []);
|
|
17
|
+
if (!e) {
|
|
18
|
+
console.warn("Unable to save shuffled choices because there's no session.");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (!a(i))
|
|
22
|
+
return u(i.map((f) => r.find((l) => l[n] === f)));
|
|
23
|
+
const o = c(r);
|
|
24
|
+
if (t && typeof t == "function")
|
|
25
|
+
try {
|
|
26
|
+
const f = u(o.map((l) => l[n]));
|
|
27
|
+
a(f) ? console.error(
|
|
28
|
+
`shuffledValues is an empty array - refusing to call updateSession. shuffledChoices: ${JSON.stringify(o)}, key: ${n}`
|
|
29
|
+
) : e.id && e.element && await t(e.id, e.element, { shuffledValues: f });
|
|
30
|
+
} catch (f) {
|
|
31
|
+
console.warn("Unable to save shuffled order for choices"), console.error(f);
|
|
32
|
+
}
|
|
33
|
+
else
|
|
34
|
+
console.warn("Unable to save shuffled choices, shuffle will happen every time.");
|
|
35
|
+
return o;
|
|
36
|
+
}
|
|
37
|
+
function d(r, e, t) {
|
|
38
|
+
return !!(r.lockChoiceOrder || t["@pie-element"]?.lockChoiceOrder || (t.role ?? "student") === "instructor");
|
|
39
|
+
}
|
|
40
|
+
function s(r, e, t) {
|
|
41
|
+
return r?.partialScoring === !1 || e?.partialScoring === !1 ? !1 : typeof t == "boolean" ? t : !0;
|
|
42
|
+
}
|
|
43
|
+
const p = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
44
|
+
__proto__: null,
|
|
45
|
+
enabled: s
|
|
46
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
47
|
+
export {
|
|
48
|
+
h as getShuffledChoices,
|
|
49
|
+
d as lockChoices,
|
|
50
|
+
p as partialScoring
|
|
51
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Partial Scoring Utilities
|
|
3
|
+
*
|
|
4
|
+
* Determines whether partial scoring should be enabled based on model and environment configuration.
|
|
5
|
+
*/
|
|
6
|
+
interface Config {
|
|
7
|
+
partialScoring?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface Environment {
|
|
10
|
+
partialScoring?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Check if partial scoring is enabled based on config and environment settings.
|
|
14
|
+
*
|
|
15
|
+
* Rules:
|
|
16
|
+
* - If model.partialScoring = false, always use dichotomous scoring (return false)
|
|
17
|
+
* - If env.partialScoring = false, always use dichotomous scoring (return false)
|
|
18
|
+
* - Otherwise, use partial scoring (return true or defaultValue)
|
|
19
|
+
*
|
|
20
|
+
* @param config - Model configuration
|
|
21
|
+
* @param env - Environment configuration
|
|
22
|
+
* @param defaultValue - Default value if neither config nor env specify a preference
|
|
23
|
+
* @returns Whether partial scoring is enabled
|
|
24
|
+
*/
|
|
25
|
+
export declare function enabled(config?: Config, env?: Environment, defaultValue?: boolean): boolean;
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=partial-scoring.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partial-scoring.d.ts","sourceRoot":"","sources":["../src/partial-scoring.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,UAAU,MAAM;IACd,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,UAAU,WAAW;IACnB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,WAAW,EAAE,YAAY,CAAC,EAAE,OAAO,GAAG,OAAO,CAU3F"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Choice Persistence Utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for managing shuffled choice order and session persistence.
|
|
5
|
+
*/
|
|
6
|
+
interface Session {
|
|
7
|
+
id?: string;
|
|
8
|
+
element?: string;
|
|
9
|
+
data?: {
|
|
10
|
+
shuffledValues?: string[];
|
|
11
|
+
};
|
|
12
|
+
shuffledValues?: string[];
|
|
13
|
+
}
|
|
14
|
+
interface Choice {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
|
17
|
+
interface Model {
|
|
18
|
+
lockChoiceOrder?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface Environment {
|
|
21
|
+
'@pie-element'?: {
|
|
22
|
+
lockChoiceOrder?: boolean;
|
|
23
|
+
};
|
|
24
|
+
role?: 'student' | 'instructor';
|
|
25
|
+
}
|
|
26
|
+
type UpdateSessionFunction = (sessionId: string, element: string, data: {
|
|
27
|
+
shuffledValues: string[];
|
|
28
|
+
}) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Get shuffled choices, either from session storage or by creating a new shuffle.
|
|
31
|
+
*
|
|
32
|
+
* This function:
|
|
33
|
+
* 1. Checks if shuffled values already exist in the session
|
|
34
|
+
* 2. If they exist, returns choices in that order
|
|
35
|
+
* 3. If they don't exist, shuffles the choices and optionally saves to session
|
|
36
|
+
*
|
|
37
|
+
* @param choices - Array of choice objects to shuffle
|
|
38
|
+
* @param session - Current session object that may contain previous shuffle order
|
|
39
|
+
* @param updateSession - Optional function to persist the shuffle order
|
|
40
|
+
* @param choiceKey - Property name to use as the unique identifier (e.g., 'value', 'id')
|
|
41
|
+
* @returns Promise resolving to shuffled choices array
|
|
42
|
+
*/
|
|
43
|
+
export declare function getShuffledChoices<T extends Choice>(choices: T[], session: Session | undefined, updateSession?: UpdateSessionFunction, choiceKey?: string): Promise<T[] | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Determine whether choice order should be locked (ordinal) or shuffled.
|
|
46
|
+
*
|
|
47
|
+
* Returns:
|
|
48
|
+
* - `true` - Choices maintain their ordinal order (as created in configure)
|
|
49
|
+
* - `false` - Choices will be shuffled (via getShuffledChoices)
|
|
50
|
+
*
|
|
51
|
+
* Logic:
|
|
52
|
+
* 1. If model.lockChoiceOrder is true, lock order
|
|
53
|
+
* 2. If env['@pie-element'].lockChoiceOrder is true, lock order
|
|
54
|
+
* 3. If role is 'instructor', lock order (instructors see ordinal order)
|
|
55
|
+
* 4. Otherwise (student role), allow shuffling
|
|
56
|
+
*
|
|
57
|
+
* @param model - Model configuration
|
|
58
|
+
* @param session - Current session (unused currently but kept for API compatibility)
|
|
59
|
+
* @param env - Environment configuration
|
|
60
|
+
* @returns Whether to lock choice order
|
|
61
|
+
*/
|
|
62
|
+
export declare function lockChoices(model: Model, _session: Session | undefined, env: Environment): boolean;
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=persistence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../src/persistence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,UAAU,OAAO;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE;QACL,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;KAC3B,CAAC;IACF,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,UAAU,MAAM;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,UAAU,KAAK;IACb,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,UAAU,WAAW;IACnB,cAAc,CAAC,EAAE;QACf,eAAe,CAAC,EAAE,OAAO,CAAC;KAC3B,CAAC;IACF,IAAI,CAAC,EAAE,SAAS,GAAG,YAAY,CAAC;CACjC;AAED,KAAK,qBAAqB,GAAG,CAC3B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE;IAAE,cAAc,EAAE,MAAM,EAAE,CAAA;CAAE,KAC/B,OAAO,CAAC,IAAI,CAAC,CAAC;AAkCnB;;;;;;;;;;;;;GAaG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,MAAM,EACvD,OAAO,EAAE,CAAC,EAAE,EACZ,OAAO,EAAE,OAAO,GAAG,SAAS,EAC5B,aAAa,CAAC,EAAE,qBAAqB,EACrC,SAAS,GAAE,MAAgB,GAC1B,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAqC1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,OAAO,GAAG,SAAS,EAC7B,GAAG,EAAE,WAAW,GACf,OAAO,CAmBT"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-element/shared-controller-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Controller utility functions for PIE elements",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo && vite build && tsc --emitDeclarationOnly",
|
|
19
|
+
"dev": "vite build --watch",
|
|
20
|
+
"test": "vitest",
|
|
21
|
+
"clean": "rm -rf dist"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pie-element/shared-types": "workspace:*"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/bun": "latest",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"vite": "^7.2.2",
|
|
30
|
+
"vitest": "^4.0.17"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT"
|
|
36
|
+
}
|