@pie-element/shared-feedback 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 +85 -0
- package/dist/defaults.d.ts +6 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/types.d.ts +29 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +34 -0
- package/src/defaults.ts +27 -0
- package/src/index.ts +115 -0
- package/src/types.ts +31 -0
- package/src/utils.ts +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @pie-element/shared-feedback
|
|
2
|
+
|
|
3
|
+
Feedback utilities for PIE elements. Provides utilities for getting feedback messages based on correctness levels.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @pie-element/shared-feedback
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { getActualFeedbackForCorrectness } from '@pie-element/shared-feedback';
|
|
17
|
+
|
|
18
|
+
// Get feedback for a correct answer
|
|
19
|
+
const message = getActualFeedbackForCorrectness('correct');
|
|
20
|
+
// Returns: 'Correct'
|
|
21
|
+
|
|
22
|
+
// Get feedback with custom configuration
|
|
23
|
+
const customMessage = getActualFeedbackForCorrectness('correct', {
|
|
24
|
+
correct: {
|
|
25
|
+
type: 'custom',
|
|
26
|
+
default: 'Correct',
|
|
27
|
+
custom: 'Excellent work!',
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
// Returns: 'Excellent work!'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Feedback Types
|
|
34
|
+
|
|
35
|
+
- `default` - Use the default feedback message
|
|
36
|
+
- `custom` - Use a custom feedback message
|
|
37
|
+
- `none` - Don't show any feedback (returns `undefined`)
|
|
38
|
+
|
|
39
|
+
### Correctness Levels
|
|
40
|
+
|
|
41
|
+
- `correct` - The answer is completely correct
|
|
42
|
+
- `incorrect` - The answer is completely incorrect
|
|
43
|
+
- `partial` - The answer is partially correct
|
|
44
|
+
- `unanswered` - No answer was provided
|
|
45
|
+
|
|
46
|
+
### API
|
|
47
|
+
|
|
48
|
+
#### `getActualFeedbackForCorrectness(correctness, feedback?)`
|
|
49
|
+
|
|
50
|
+
Get the feedback message for a specific correctness level (synchronous).
|
|
51
|
+
|
|
52
|
+
**Parameters:**
|
|
53
|
+
- `correctness`: `'correct' | 'incorrect' | 'partial' | 'unanswered' | 'partially-correct'`
|
|
54
|
+
- `feedback`: `Partial<Feedback>` (optional) - Custom feedback configuration
|
|
55
|
+
|
|
56
|
+
**Returns:** `string | undefined`
|
|
57
|
+
|
|
58
|
+
#### `getFeedbackForCorrectness(correctness, feedback?)`
|
|
59
|
+
|
|
60
|
+
Async version of `getActualFeedbackForCorrectness`.
|
|
61
|
+
|
|
62
|
+
**Parameters:**
|
|
63
|
+
- `correctness`: `'correct' | 'incorrect' | 'partial' | 'unanswered' | 'partially-correct'`
|
|
64
|
+
- `feedback`: `Partial<Feedback>` (optional) - Custom feedback configuration
|
|
65
|
+
|
|
66
|
+
**Returns:** `Promise<string | undefined>`
|
|
67
|
+
|
|
68
|
+
## TypeScript
|
|
69
|
+
|
|
70
|
+
This package is written in TypeScript and includes type definitions.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import type { Feedback, FeedbackConfig, Correctness } from '@pie-element/shared-feedback';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Default Feedback Messages
|
|
77
|
+
|
|
78
|
+
- **Correct**: "Correct"
|
|
79
|
+
- **Incorrect**: "Incorrect"
|
|
80
|
+
- **Partial**: "Nearly"
|
|
81
|
+
- **Unanswered**: "You have not entered a response"
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,QAqB7B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @pie-element/shared-feedback
|
|
3
|
+
*
|
|
4
|
+
* Feedback utilities for PIE elements
|
|
5
|
+
* Provides utilities for getting feedback messages based on correctness levels
|
|
6
|
+
*
|
|
7
|
+
* Ported from @pie-lib/feedback with TypeScript types and improved structure
|
|
8
|
+
*/
|
|
9
|
+
export * from './types';
|
|
10
|
+
export * from './defaults';
|
|
11
|
+
export * from './utils';
|
|
12
|
+
import type { Correctness, Feedback, FeedbackConfig } from './types';
|
|
13
|
+
/**
|
|
14
|
+
* Get the feedback message for a specific correctness level (async)
|
|
15
|
+
*
|
|
16
|
+
* @param correctness - The correctness level ('correct', 'incorrect', 'partial', 'unanswered')
|
|
17
|
+
* @param feedback - Partial feedback configuration (merged with defaults)
|
|
18
|
+
* @returns Promise resolving to the feedback message or undefined if type is 'none'
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const message = await getFeedbackForCorrectness('correct', {
|
|
23
|
+
* correct: { type: 'custom', default: 'Correct', custom: 'Great job!' }
|
|
24
|
+
* });
|
|
25
|
+
* // Returns: 'Great job!'
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function getFeedbackForCorrectness(correctness: Correctness | 'partially-correct', feedback?: Partial<Feedback>): Promise<string | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the feedback message from a feedback configuration (async)
|
|
31
|
+
*
|
|
32
|
+
* @param config - The feedback configuration
|
|
33
|
+
* @param fallback - Fallback message if config type is not valid
|
|
34
|
+
* @returns Promise resolving to the feedback message or undefined if type is 'none'
|
|
35
|
+
*/
|
|
36
|
+
export declare function getFeedback(config: FeedbackConfig, fallback: string): Promise<string | undefined>;
|
|
37
|
+
/**
|
|
38
|
+
* Get the feedback message for a specific correctness level (synchronous)
|
|
39
|
+
*
|
|
40
|
+
* This is the recommended function to use for most cases as it's synchronous
|
|
41
|
+
* and more predictable than the Promise-based version.
|
|
42
|
+
*
|
|
43
|
+
* @param correctness - The correctness level ('correct', 'incorrect', 'partial', 'unanswered')
|
|
44
|
+
* @param feedback - Partial feedback configuration (merged with defaults)
|
|
45
|
+
* @returns The feedback message or undefined if type is 'none'
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const message = getActualFeedbackForCorrectness('correct', {
|
|
50
|
+
* correct: { type: 'custom', default: 'Correct', custom: 'Great job!' }
|
|
51
|
+
* });
|
|
52
|
+
* // Returns: 'Great job!'
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function getActualFeedbackForCorrectness(correctness: Correctness | 'partially-correct', feedback?: Partial<Feedback>): string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Get the feedback message from a feedback configuration (synchronous)
|
|
58
|
+
*
|
|
59
|
+
* @param config - The feedback configuration
|
|
60
|
+
* @param fallback - Fallback message if config type is not valid
|
|
61
|
+
* @returns The feedback message or undefined if type is 'none'
|
|
62
|
+
*/
|
|
63
|
+
export declare function getActualFeedback(config: FeedbackConfig, fallback: string): string | undefined;
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AAExB,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAIrE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,WAAW,GAAG,mBAAmB,EAC9C,QAAQ,GAAE,OAAO,CAAC,QAAQ,CAAM,GAC/B,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAS7B;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,cAAc,EACtB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAU7B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,+BAA+B,CAC7C,WAAW,EAAE,WAAW,GAAG,mBAAmB,EAC9C,QAAQ,GAAE,OAAO,CAAC,QAAQ,CAAM,GAC/B,MAAM,GAAG,SAAS,CAOpB;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,cAAc,EACtB,QAAQ,EAAE,MAAM,GACf,MAAM,GAAG,SAAS,CAMpB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const c = {
|
|
2
|
+
correct: {
|
|
3
|
+
type: "default",
|
|
4
|
+
default: "Correct",
|
|
5
|
+
custom: "Correct"
|
|
6
|
+
},
|
|
7
|
+
incorrect: {
|
|
8
|
+
type: "default",
|
|
9
|
+
default: "Incorrect",
|
|
10
|
+
custom: "Incorrect"
|
|
11
|
+
},
|
|
12
|
+
partial: {
|
|
13
|
+
type: "default",
|
|
14
|
+
default: "Nearly",
|
|
15
|
+
custom: "Nearly"
|
|
16
|
+
},
|
|
17
|
+
unanswered: {
|
|
18
|
+
type: "default",
|
|
19
|
+
default: "You have not entered a response",
|
|
20
|
+
custom: "You have not entered a response"
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
function u(e) {
|
|
24
|
+
return e === "partially-correct" ? "partial" : e;
|
|
25
|
+
}
|
|
26
|
+
function f(e, t = {}) {
|
|
27
|
+
return new Promise((r) => {
|
|
28
|
+
const n = { ...c, ...t }, o = u(e), a = n[o], s = c[o];
|
|
29
|
+
d(a, s[a.type]).then(r);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function d(e, t) {
|
|
33
|
+
return new Promise((r) => {
|
|
34
|
+
if (!e || e.type === "none") {
|
|
35
|
+
r(void 0);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const n = e[e.type] || t;
|
|
39
|
+
r(n);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function i(e, t = {}) {
|
|
43
|
+
const r = { ...c, ...t }, n = u(e), o = r[n], a = c[n];
|
|
44
|
+
return l(o, a[o.type]);
|
|
45
|
+
}
|
|
46
|
+
function l(e, t) {
|
|
47
|
+
if (!(!e || e.type === "none"))
|
|
48
|
+
return e[e.type] || t;
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
c as defaultFeedback,
|
|
52
|
+
l as getActualFeedback,
|
|
53
|
+
i as getActualFeedbackForCorrectness,
|
|
54
|
+
d as getFeedback,
|
|
55
|
+
f as getFeedbackForCorrectness,
|
|
56
|
+
u as normalizeCorrectness
|
|
57
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feedback type configuration
|
|
3
|
+
*/
|
|
4
|
+
export type FeedbackType = 'default' | 'none' | 'custom';
|
|
5
|
+
/**
|
|
6
|
+
* Correctness levels
|
|
7
|
+
*/
|
|
8
|
+
export type Correctness = 'correct' | 'incorrect' | 'partial' | 'unanswered';
|
|
9
|
+
/**
|
|
10
|
+
* Feedback configuration for a specific correctness level
|
|
11
|
+
*/
|
|
12
|
+
export interface FeedbackConfig {
|
|
13
|
+
/** The type of feedback to show */
|
|
14
|
+
type: FeedbackType;
|
|
15
|
+
/** The default feedback message */
|
|
16
|
+
default: string;
|
|
17
|
+
/** The custom feedback message (if type is 'custom') */
|
|
18
|
+
custom: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Complete feedback configuration for all correctness levels
|
|
22
|
+
*/
|
|
23
|
+
export interface Feedback {
|
|
24
|
+
correct: FeedbackConfig;
|
|
25
|
+
incorrect: FeedbackConfig;
|
|
26
|
+
partial: FeedbackConfig;
|
|
27
|
+
unanswered: FeedbackConfig;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,GAAG,YAAY,CAAC;AAE7E;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,mCAAmC;IACnC,IAAI,EAAE,YAAY,CAAC;IACnB,mCAAmC;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,cAAc,CAAC;IACxB,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,UAAU,EAAE,cAAc,CAAC;CAC5B"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Correctness } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Normalize correctness value to standard format
|
|
4
|
+
* Converts 'partially-correct' to 'partial' for consistency
|
|
5
|
+
*/
|
|
6
|
+
export declare function normalizeCorrectness(correctness: string): Correctness;
|
|
7
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW,CAKrE"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-element/shared-feedback",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Feedback utilities 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
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo && vite build && tsc --emitDeclarationOnly",
|
|
20
|
+
"dev": "vite build --watch",
|
|
21
|
+
"test": "vitest",
|
|
22
|
+
"clean": "rm -rf dist"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/bun": "latest",
|
|
26
|
+
"typescript": "^5.9.3",
|
|
27
|
+
"vite": "^7.2.2",
|
|
28
|
+
"vitest": "^4.0.17"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT"
|
|
34
|
+
}
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Feedback } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default feedback messages for all correctness levels
|
|
5
|
+
*/
|
|
6
|
+
export const defaultFeedback: Feedback = {
|
|
7
|
+
correct: {
|
|
8
|
+
type: 'default',
|
|
9
|
+
default: 'Correct',
|
|
10
|
+
custom: 'Correct',
|
|
11
|
+
},
|
|
12
|
+
incorrect: {
|
|
13
|
+
type: 'default',
|
|
14
|
+
default: 'Incorrect',
|
|
15
|
+
custom: 'Incorrect',
|
|
16
|
+
},
|
|
17
|
+
partial: {
|
|
18
|
+
type: 'default',
|
|
19
|
+
default: 'Nearly',
|
|
20
|
+
custom: 'Nearly',
|
|
21
|
+
},
|
|
22
|
+
unanswered: {
|
|
23
|
+
type: 'default',
|
|
24
|
+
default: 'You have not entered a response',
|
|
25
|
+
custom: 'You have not entered a response',
|
|
26
|
+
},
|
|
27
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @pie-element/shared-feedback
|
|
3
|
+
*
|
|
4
|
+
* Feedback utilities for PIE elements
|
|
5
|
+
* Provides utilities for getting feedback messages based on correctness levels
|
|
6
|
+
*
|
|
7
|
+
* Ported from @pie-lib/feedback with TypeScript types and improved structure
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export * from './types';
|
|
11
|
+
export * from './defaults';
|
|
12
|
+
export * from './utils';
|
|
13
|
+
|
|
14
|
+
import type { Correctness, Feedback, FeedbackConfig } from './types';
|
|
15
|
+
import { defaultFeedback } from './defaults';
|
|
16
|
+
import { normalizeCorrectness } from './utils';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the feedback message for a specific correctness level (async)
|
|
20
|
+
*
|
|
21
|
+
* @param correctness - The correctness level ('correct', 'incorrect', 'partial', 'unanswered')
|
|
22
|
+
* @param feedback - Partial feedback configuration (merged with defaults)
|
|
23
|
+
* @returns Promise resolving to the feedback message or undefined if type is 'none'
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const message = await getFeedbackForCorrectness('correct', {
|
|
28
|
+
* correct: { type: 'custom', default: 'Correct', custom: 'Great job!' }
|
|
29
|
+
* });
|
|
30
|
+
* // Returns: 'Great job!'
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function getFeedbackForCorrectness(
|
|
34
|
+
correctness: Correctness | 'partially-correct',
|
|
35
|
+
feedback: Partial<Feedback> = {}
|
|
36
|
+
): Promise<string | undefined> {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const merged = { ...defaultFeedback, ...feedback };
|
|
39
|
+
const normalized = normalizeCorrectness(correctness);
|
|
40
|
+
const config = merged[normalized];
|
|
41
|
+
const defaultConfig = defaultFeedback[normalized];
|
|
42
|
+
|
|
43
|
+
getFeedback(config, defaultConfig[config.type as keyof FeedbackConfig] as string).then(resolve);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the feedback message from a feedback configuration (async)
|
|
49
|
+
*
|
|
50
|
+
* @param config - The feedback configuration
|
|
51
|
+
* @param fallback - Fallback message if config type is not valid
|
|
52
|
+
* @returns Promise resolving to the feedback message or undefined if type is 'none'
|
|
53
|
+
*/
|
|
54
|
+
export function getFeedback(
|
|
55
|
+
config: FeedbackConfig,
|
|
56
|
+
fallback: string
|
|
57
|
+
): Promise<string | undefined> {
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
if (!config || config.type === 'none') {
|
|
60
|
+
resolve(undefined);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const message = (config[config.type as keyof FeedbackConfig] as string) || fallback;
|
|
65
|
+
resolve(message);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the feedback message for a specific correctness level (synchronous)
|
|
71
|
+
*
|
|
72
|
+
* This is the recommended function to use for most cases as it's synchronous
|
|
73
|
+
* and more predictable than the Promise-based version.
|
|
74
|
+
*
|
|
75
|
+
* @param correctness - The correctness level ('correct', 'incorrect', 'partial', 'unanswered')
|
|
76
|
+
* @param feedback - Partial feedback configuration (merged with defaults)
|
|
77
|
+
* @returns The feedback message or undefined if type is 'none'
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const message = getActualFeedbackForCorrectness('correct', {
|
|
82
|
+
* correct: { type: 'custom', default: 'Correct', custom: 'Great job!' }
|
|
83
|
+
* });
|
|
84
|
+
* // Returns: 'Great job!'
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function getActualFeedbackForCorrectness(
|
|
88
|
+
correctness: Correctness | 'partially-correct',
|
|
89
|
+
feedback: Partial<Feedback> = {}
|
|
90
|
+
): string | undefined {
|
|
91
|
+
const merged = { ...defaultFeedback, ...feedback };
|
|
92
|
+
const normalized = normalizeCorrectness(correctness);
|
|
93
|
+
const config = merged[normalized];
|
|
94
|
+
const defaultConfig = defaultFeedback[normalized];
|
|
95
|
+
|
|
96
|
+
return getActualFeedback(config, defaultConfig[config.type as keyof FeedbackConfig] as string);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the feedback message from a feedback configuration (synchronous)
|
|
101
|
+
*
|
|
102
|
+
* @param config - The feedback configuration
|
|
103
|
+
* @param fallback - Fallback message if config type is not valid
|
|
104
|
+
* @returns The feedback message or undefined if type is 'none'
|
|
105
|
+
*/
|
|
106
|
+
export function getActualFeedback(
|
|
107
|
+
config: FeedbackConfig,
|
|
108
|
+
fallback: string
|
|
109
|
+
): string | undefined {
|
|
110
|
+
if (!config || config.type === 'none') {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return (config[config.type as keyof FeedbackConfig] as string) || fallback;
|
|
115
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feedback type configuration
|
|
3
|
+
*/
|
|
4
|
+
export type FeedbackType = 'default' | 'none' | 'custom';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Correctness levels
|
|
8
|
+
*/
|
|
9
|
+
export type Correctness = 'correct' | 'incorrect' | 'partial' | 'unanswered';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Feedback configuration for a specific correctness level
|
|
13
|
+
*/
|
|
14
|
+
export interface FeedbackConfig {
|
|
15
|
+
/** The type of feedback to show */
|
|
16
|
+
type: FeedbackType;
|
|
17
|
+
/** The default feedback message */
|
|
18
|
+
default: string;
|
|
19
|
+
/** The custom feedback message (if type is 'custom') */
|
|
20
|
+
custom: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Complete feedback configuration for all correctness levels
|
|
25
|
+
*/
|
|
26
|
+
export interface Feedback {
|
|
27
|
+
correct: FeedbackConfig;
|
|
28
|
+
incorrect: FeedbackConfig;
|
|
29
|
+
partial: FeedbackConfig;
|
|
30
|
+
unanswered: FeedbackConfig;
|
|
31
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Correctness } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalize correctness value to standard format
|
|
5
|
+
* Converts 'partially-correct' to 'partial' for consistency
|
|
6
|
+
*/
|
|
7
|
+
export function normalizeCorrectness(correctness: string): Correctness {
|
|
8
|
+
if (correctness === 'partially-correct') {
|
|
9
|
+
return 'partial';
|
|
10
|
+
}
|
|
11
|
+
return correctness as Correctness;
|
|
12
|
+
}
|