@media-quest/builder 0.0.26 → 0.0.28

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,151 +1,308 @@
1
- import { SumScore } from "./sum-score";
2
- import { SumScoreVariableDto } from "./sum-score-variable";
3
- import { SumScoreVariableID } from "../primitives/ID";
4
- import { SumScoreAnswer } from "./sum-score-answer";
5
-
6
- const createAnswer = (value: number): SumScoreAnswer => {
7
- const varId = "v" + value;
8
- const varLabel = "label for " + varId;
9
- const valueLabel = "label for value " + value;
10
- return {
11
- varId,
12
- varLabel,
13
- value,
14
- valueLabel,
15
- };
16
- };
17
-
18
- const createSumScore = (basedOn: SumScoreVariableDto["basedOn"]) => {
19
- const id = SumScoreVariableID.create();
20
- const sumVar1: SumScoreVariableDto = {
21
- id,
22
- name: "",
23
- description: "",
24
- useAvg: true,
25
- basedOn,
26
- };
27
- return sumVar1;
28
- };
29
-
30
- const a0 = createAnswer(0);
31
- const a1 = createAnswer(1);
32
- const a2 = createAnswer(2);
33
- const a3 = createAnswer(3);
34
- const a4 = createAnswer(4);
35
- const a5 = createAnswer(5);
36
- const a6 = createAnswer(6);
37
- const a7 = createAnswer(7);
38
- const a8 = createAnswer(8);
39
- const a9 = createAnswer(9);
40
-
41
- const all = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9];
42
-
43
- describe("Sum score sum-score.", () => {
44
- test("Is allowed SumScoreValue", () => {
45
- expect(SumScore.isAllowedValue(-1)).toBeFalsy();
46
- expect(SumScore.isAllowedValue(0)).toBeTruthy();
47
- expect(SumScore.isAllowedValue(1)).toBeTruthy();
48
- expect(SumScore.isAllowedValue(2)).toBeTruthy();
49
- expect(SumScore.isAllowedValue(3)).toBeTruthy();
50
- expect(SumScore.isAllowedValue(4)).toBeTruthy();
51
- expect(SumScore.isAllowedValue(5)).toBeTruthy();
52
- expect(SumScore.isAllowedValue(6)).toBeTruthy();
53
- expect(SumScore.isAllowedValue(7)).toBeTruthy();
54
- expect(SumScore.isAllowedValue(8)).toBeTruthy();
55
- expect(SumScore.isAllowedValue(9)).toBeFalsy();
56
- expect(SumScore.isAllowedValue(999)).toBeFalsy();
57
- });
58
-
59
- test("Will calculate 2", () => {
60
- const ss1 = createSumScore([
61
- { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
62
- { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
63
- { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
64
- ]);
65
- const score = SumScore.calculate(ss1, all);
66
- expect(score.sumScore).toBe(3);
67
- expect(score.basedOn.length).toBe(3);
68
- expect(score.avg).toBe(1.5);
69
- expect(score.skippedBy9Count).toBe(1);
70
- });
71
- test("Will calculate 5 values", () => {
72
- const ss1 = createSumScore([
73
- { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
74
- { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
75
- { varId: a4.varId, varLabel: a4.varLabel, weight: 1 },
76
- { varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
77
- { varId: a6.varId, varLabel: a6.varLabel, weight: 1 },
78
- { varId: a9.varId, varLabel: a9.varLabel, weight: 0.5 },
79
- ]);
80
- const score = SumScore.calculate(ss1, all);
81
- expect(score.sumScore).toBe(18);
82
- expect(score.avg).toBe(3.6);
83
- const basedOn1 = score.basedOn[0];
84
- expect(basedOn1.value).toBe(a1.value);
85
- expect(basedOn1.weight).toBe(1);
86
- expect(basedOn1.varId).toBe(a1.varId);
87
- expect(basedOn1.varLabel).toBe(a1.varLabel);
88
- });
89
- test("Will not include 9 in includedAnswersCount", () => {
90
- const ss1 = createSumScore([
91
- { varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
92
- { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
93
- { varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
94
- { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
95
- ]);
96
- const score = SumScore.calculate(ss1, all);
97
- expect(score.sumScore).toBe(6);
98
- expect(score.avg).toBe(2);
99
- expect(score.includedAnswerCount).toBe(3);
100
- });
101
- test("Will calculate weight", () => {
102
- const ss1 = createSumScore([
103
- { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
104
- { varId: a8.varId, varLabel: a8.varLabel, weight: 0.5 },
105
- ]);
106
- const score = SumScore.calculate(ss1, all);
107
- expect(score.sumScore).toBe(5);
108
- });
109
- test("Will calculate many weighted variables.", () => {
110
- const ss1 = createSumScore([
111
- { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
112
- { varId: a2.varId, varLabel: a2.varLabel, weight: 0.5 },
113
- { varId: a5.varId, varLabel: a5.varLabel, weight: 1.2 },
114
- { varId: a8.varId, varLabel: a8.varLabel, weight: 2 },
115
- ]);
116
- const score = SumScore.calculate(ss1, all);
117
- expect(score.sumScore).toBe(24);
118
- });
119
- test("Will count missing answers (missingAnswersCount)", () => {
120
- const ss1 = createSumScore([
121
- { varId: a1.varId, varLabel: a1.varLabel },
122
- { varId: a2.varId, varLabel: a2.varLabel },
123
- { varId: a3.varId, varLabel: a3.varLabel },
124
- { varId: a5.varId, varLabel: a5.varLabel },
125
- { varId: a9.varId, varLabel: a9.varLabel },
126
- ]);
127
- const missing5InDataSet = [a1, a2, a8];
128
- const score = SumScore.calculate(ss1, missing5InDataSet);
129
- expect(score.sumScore).toBe(3);
130
- expect(score.includedAnswerCount).toBe(2);
131
- expect(score.missingAnswerCount).toBe(3);
132
- expect(score.avg).toBe(1.5);
133
- expect(score.basedOn.length).toBe(2);
134
- expect(score.avg).toBe(1.5);
135
- expect(score.errorMessages.length).toBe(0);
136
- });
137
- test("includedAnswerCount + missingAnswerCount + skippedBy9Count = SumScoreVariable.basedOn.length", () => {
138
- const ss1 = createSumScore([
139
- { varId: a0.varId, varLabel: a0.varLabel },
140
- { varId: a1.varId, varLabel: a1.varLabel },
141
- { varId: a2.varId, varLabel: a2.varLabel },
142
- { varId: a9.varId, varLabel: a9.varLabel },
143
- ]);
144
- const answers = [a1, a7, a8, a9];
145
- const score = SumScore.calculate(ss1, answers);
146
-
147
- expect(score.includedAnswerCount + score.missingAnswerCount + score.skippedBy9Count).toBe(
148
- ss1.basedOn.length,
149
- );
150
- });
151
- });
1
+ import { SumScore } from "./sum-score";
2
+ import { SumScoreVariable, SumScoreVariableDto } from "./sum-score-variable";
3
+ import { SchemaID, SumScoreVariableID } from "../primitives/ID";
4
+ import { SumScoreAnswer } from "./sum-score-answer";
5
+ import { BuilderSchema } from "../Builder-schema";
6
+ import { SchemaPrefix } from "../primitives/schema-prefix";
7
+ import { CodeBook } from "../code-book/codebook";
8
+ import { BuilderPage } from "../page/Builder-page";
9
+ import { VarID } from "../primitives/varID";
10
+
11
+ const createAnswer = (value: number): SumScoreAnswer => {
12
+ const varId = "v" + value;
13
+ const varLabel = "label for " + varId;
14
+ const valueLabel = "label for value " + value;
15
+ return {
16
+ varId,
17
+ varLabel,
18
+ value,
19
+ valueLabel,
20
+ };
21
+ };
22
+
23
+ const createSumScore = () => {
24
+ const id = SumScoreVariableID.create();
25
+ const sumVar1: SumScoreVariableDto = {
26
+ id,
27
+ name: "",
28
+ description: "",
29
+ useAvg: true,
30
+ };
31
+ return sumVar1;
32
+ };
33
+
34
+ const a0 = createAnswer(0);
35
+ const a1 = createAnswer(1);
36
+ const a2 = createAnswer(2);
37
+ const a3 = createAnswer(3);
38
+ const a4 = createAnswer(4);
39
+ const a5 = createAnswer(5);
40
+ const a6 = createAnswer(6);
41
+ const a7 = createAnswer(7);
42
+ const a8 = createAnswer(8);
43
+ const a9 = createAnswer(9);
44
+
45
+ const all = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9];
46
+
47
+ describe("Sum score sum-score.", () => {
48
+ test("Is allowed SumScoreValue", () => {
49
+ expect(SumScore.isAllowedValue(-1)).toBeFalsy();
50
+ expect(SumScore.isAllowedValue(0)).toBeTruthy();
51
+ expect(SumScore.isAllowedValue(1)).toBeTruthy();
52
+ expect(SumScore.isAllowedValue(2)).toBeTruthy();
53
+ expect(SumScore.isAllowedValue(3)).toBeTruthy();
54
+ expect(SumScore.isAllowedValue(4)).toBeTruthy();
55
+ expect(SumScore.isAllowedValue(5)).toBeTruthy();
56
+ expect(SumScore.isAllowedValue(6)).toBeTruthy();
57
+ expect(SumScore.isAllowedValue(7)).toBeTruthy();
58
+ expect(SumScore.isAllowedValue(8)).toBeTruthy();
59
+ expect(SumScore.isAllowedValue(9)).toBeFalsy();
60
+ expect(SumScore.isAllowedValue(999)).toBeFalsy();
61
+ });
62
+
63
+ test("Will calculate 2", () => {
64
+ const ss1 = createSumScore();
65
+ const basedOn = [
66
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
67
+ { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
68
+ { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
69
+ ];
70
+
71
+ const score = SumScore.calculate(ss1, basedOn, all);
72
+ expect(score.sumScore).toBe(3);
73
+ expect(score.basedOn.length).toBe(3);
74
+ expect(score.avg).toBe(1.5);
75
+ expect(score.skippedBy9Count).toBe(1);
76
+ });
77
+ test("Will calculate 5 values", () => {
78
+ const ss1 = createSumScore();
79
+ const basedOn = [
80
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
81
+ { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
82
+ { varId: a4.varId, varLabel: a4.varLabel, weight: 1 },
83
+ { varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
84
+ { varId: a6.varId, varLabel: a6.varLabel, weight: 1 },
85
+ { varId: a9.varId, varLabel: a9.varLabel, weight: 0.5 },
86
+ ];
87
+ const score = SumScore.calculate(ss1, basedOn, all);
88
+ expect(score.sumScore).toBe(18);
89
+ expect(score.avg).toBe(3.6);
90
+ const basedOn1 = score.basedOn[0];
91
+ expect(basedOn1.value).toBe(a1.value);
92
+ expect(basedOn1.weight).toBe(1);
93
+ expect(basedOn1.varId).toBe(a1.varId);
94
+ expect(basedOn1.varLabel).toBe(a1.varLabel);
95
+ });
96
+ test("Will not include 9 in includedAnswersCount", () => {
97
+ const ss1 = createSumScore();
98
+ const basedOn = [
99
+ { varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
100
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
101
+ { varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
102
+ { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
103
+ ];
104
+ const score = SumScore.calculate(ss1, basedOn, all);
105
+ expect(score.sumScore).toBe(6);
106
+ expect(score.avg).toBe(2);
107
+ expect(score.includedAnswerCount).toBe(3);
108
+ });
109
+ test("Will calculate weight", () => {
110
+ const ss1 = createSumScore();
111
+ const basedOn = [
112
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
113
+ { varId: a8.varId, varLabel: a8.varLabel, weight: 0.5 },
114
+ ];
115
+ const score = SumScore.calculate(ss1, basedOn, all);
116
+ expect(score.sumScore).toBe(5);
117
+ });
118
+ test("Will calculate many weighted variables.", () => {
119
+ const ss1 = createSumScore();
120
+ const basedOn = [
121
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
122
+ { varId: a2.varId, varLabel: a2.varLabel, weight: 0.5 },
123
+ { varId: a5.varId, varLabel: a5.varLabel, weight: 1.2 },
124
+ { varId: a8.varId, varLabel: a8.varLabel, weight: 2 },
125
+ ];
126
+ const score = SumScore.calculate(ss1, basedOn, all);
127
+ expect(score.sumScore).toBe(24);
128
+ });
129
+ test("Will count missing answers (missingAnswersCount)", () => {
130
+ const ss1 = createSumScore();
131
+ const basedOn = [
132
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
133
+ { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
134
+ { varId: a3.varId, varLabel: a3.varLabel, weight: 1 },
135
+ { varId: a5.varId, varLabel: a5.varLabel, weight: 1 },
136
+ { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
137
+ ];
138
+ const missing5InDataSet = [a1, a2, a8];
139
+ const score = SumScore.calculate(ss1, basedOn, missing5InDataSet);
140
+ expect(score.sumScore).toBe(3);
141
+ expect(score.includedAnswerCount).toBe(2);
142
+ expect(score.missingAnswerCount).toBe(3);
143
+ expect(score.avg).toBe(1.5);
144
+ expect(score.basedOn.length).toBe(2);
145
+ expect(score.avg).toBe(1.5);
146
+ expect(score.errorMessages.length).toBe(0);
147
+ });
148
+ test("includedAnswerCount + missingAnswerCount + skippedBy9Count = SumScoreVariable.basedOn.length", () => {
149
+ const ss1 = createSumScore();
150
+ const basedOn = [
151
+ { varId: a0.varId, varLabel: a0.varLabel, weight: 1 },
152
+ { varId: a1.varId, varLabel: a1.varLabel, weight: 1 },
153
+ { varId: a2.varId, varLabel: a2.varLabel, weight: 1 },
154
+ { varId: a9.varId, varLabel: a9.varLabel, weight: 1 },
155
+ ];
156
+ const answers = [a1, a7, a8, a9];
157
+ const score = SumScore.calculate(ss1, basedOn, answers);
158
+
159
+ expect(score.includedAnswerCount + score.missingAnswerCount + score.skippedBy9Count).toBe(
160
+ basedOn.length,
161
+ );
162
+ });
163
+ test("variable will also have copy of all questions it used in.", () => {
164
+ const prefix = SchemaPrefix.fromValueOrThrow("dep4");
165
+ const schema = BuilderSchema.create(SchemaID.create(), "testing-dep4", prefix.value);
166
+ const p1 = schema.addPage("question");
167
+ const p2 = schema.addPage("question");
168
+ const p3 = schema.addPage("question");
169
+
170
+ // Setting question text
171
+ p1.mainText.text = "q1 text";
172
+ p2.mainText.text = "q2 text";
173
+ p3.mainText.text = "q3 text";
174
+ const v1 = schema.sumScoreVariableCreate({
175
+ name: "ss1",
176
+ description: "ss1-desc",
177
+ useAvg: true,
178
+ });
179
+ const v2 = schema.sumScoreVariableCreate({
180
+ name: "ss2",
181
+ description: "ss2-desc",
182
+ useAvg: true,
183
+ });
184
+ const v3 = schema.sumScoreVariableCreate({
185
+ name: "ss3",
186
+ description: "ss3-desc",
187
+ useAvg: true,
188
+ });
189
+ schema.sumScoreVariableAddToPage(v1, p1, 1);
190
+ schema.sumScoreVariableAddToPage(v1, p2, 1);
191
+ schema.sumScoreVariableAddToPage(v2, p2, 1);
192
+ schema.sumScoreVariableAddToPage(v3, p1, 1);
193
+ schema.sumScoreVariableAddToPage(v3, p2, 1);
194
+ schema.sumScoreVariableAddToPage(v3, p3, 1);
195
+
196
+ expect(v1.usedIn.length).toBe(2);
197
+ expect(v2.usedIn.length).toBe(1);
198
+ expect(v3.usedIn.length).toBe(3);
199
+ schema.sumScoreVariableDeleteFromPage(p3.id, v3.id);
200
+ expect(v3.usedIn.length).toBe(2);
201
+
202
+ schema.sumScoreVariableDeleteFromPage(p2.id, v3.id);
203
+ expect(v3.usedIn.length).toBe(1);
204
+
205
+ const json = schema.toJson();
206
+ const clone = BuilderSchema.fromJson(json);
207
+ const clonedVariables = clone.sumScoreVariables;
208
+ expect(clonedVariables.length).toBe(3);
209
+
210
+ const v1Clone = clonedVariables.find((v) => v.id === v1.id) as SumScoreVariable;
211
+ const v2Clone = clonedVariables.find((v) => v.id === v2.id) as SumScoreVariable;
212
+ const v3Clone = clonedVariables.find((v) => v.id === v3.id) as SumScoreVariable;
213
+ expect(v1Clone.usedIn.length).toBe(2);
214
+ expect(v2Clone.usedIn.length).toBe(1);
215
+ expect(v3Clone.usedIn.length).toBe(1);
216
+ });
217
+
218
+ test("calculateAll", () => {
219
+ const prefix = SchemaPrefix.fromValueOrThrow("angst");
220
+ const schema = BuilderSchema.create(SchemaID.create(), "testing", prefix.value);
221
+ const p1 = schema.addPage("question");
222
+ const p2 = schema.addPage("question");
223
+ const p3 = schema.addPage("question");
224
+
225
+ // Setting question text
226
+ p1.mainText.text = "q1";
227
+ p2.mainText.text = "q2";
228
+ p3.mainText.text = "q3";
229
+ const v1 = schema.sumScoreVariableCreate({
230
+ name: "ss1",
231
+ description: "ss1-desc",
232
+ useAvg: true,
233
+ });
234
+ const v2 = schema.sumScoreVariableCreate({
235
+ name: "ss2",
236
+ description: "ss2-desc",
237
+ useAvg: true,
238
+ });
239
+ const v3 = schema.sumScoreVariableCreate({
240
+ name: "ss3",
241
+ description: "ss3-desc",
242
+ useAvg: true,
243
+ });
244
+ const success_v1_p1 = schema.sumScoreVariableAddToPage(v1, p1, 1);
245
+ const success_v1_p2 = schema.sumScoreVariableAddToPage(v1, p2, 1);
246
+ const success_v2_p2 = schema.sumScoreVariableAddToPage(v2, p2, 1);
247
+ const success_v3_p1 = schema.sumScoreVariableAddToPage(v3, p1, 1);
248
+ const success_v3_p2 = schema.sumScoreVariableAddToPage(v3, p2, 1);
249
+ const success_v3_p3 = schema.sumScoreVariableAddToPage(v3, p3, 1);
250
+ expect(success_v1_p1).toBeTruthy();
251
+ expect(success_v1_p2).toBeTruthy();
252
+ expect(success_v2_p2).toBeTruthy();
253
+ expect(success_v3_p1).toBeTruthy();
254
+ expect(success_v3_p2).toBeTruthy();
255
+ expect(success_v3_p3).toBeTruthy();
256
+
257
+ // const v = schema.
258
+ const p1_questionVariable = p1.getQuestionVariables(prefix, 0)[0];
259
+ const p2_questionVariable = p2.getQuestionVariables(prefix, 1)[0];
260
+ const p3_questionVariable = p3.getQuestionVariables(prefix, 2)[0];
261
+
262
+ const ans1: SumScoreAnswer = {
263
+ varId: p1_questionVariable.varId,
264
+ value: 1,
265
+ varLabel: p1_questionVariable.label,
266
+ valueLabel: "Ja",
267
+ };
268
+
269
+ const ans2: SumScoreAnswer = {
270
+ varId: p2_questionVariable.varId,
271
+ value: 2,
272
+ varLabel: p2_questionVariable.label,
273
+ valueLabel: "Ja",
274
+ };
275
+ const ans3: SumScoreAnswer = {
276
+ varId: p3_questionVariable.varId,
277
+ value: 3,
278
+ varLabel: p3_questionVariable.label,
279
+ valueLabel: "Ja",
280
+ };
281
+
282
+ const sumScores = SumScore.calculateAll(schema.toJson(), [ans1, ans2, ans3]);
283
+ const ss1 = sumScores[0] as SumScore;
284
+ const ss2 = sumScores[1] as SumScore;
285
+ const ss3 = sumScores[2] as SumScore;
286
+
287
+ expect(sumScores.length).toBe(3);
288
+ // BASED ON IS WORKING.
289
+ expect(ss1.basedOn.length).toBe(2);
290
+ expect(ss2.basedOn.length).toBe(1);
291
+ expect(ss3.basedOn.length).toBe(3);
292
+ const isBasedOn = (sumScore: SumScore, variableId: string, variableLabel: string) => {
293
+ const found = sumScore.basedOn.find((basedOnItem) => basedOnItem.varId === variableId);
294
+
295
+ expect(found?.varId).toBe(variableId);
296
+ expect(found?.varLabel).toBe(variableLabel);
297
+ };
298
+ isBasedOn(ss1, ans1.varId, p1_questionVariable.label);
299
+ isBasedOn(ss1, ans2.varId, p2_questionVariable.label);
300
+ isBasedOn(ss2, ans2.varId, p2_questionVariable.label);
301
+ isBasedOn(ss3, ans1.varId, p1_questionVariable.label);
302
+ isBasedOn(ss3, ans2.varId, p2_questionVariable.label);
303
+ isBasedOn(ss3, ans3.varId, p3_questionVariable.label);
304
+ expect(ss1.sumScore).toBe(3);
305
+ expect(ss2.sumScore).toBe(2);
306
+ expect(ss3.sumScore).toBe(6);
307
+ });
308
+ });
@@ -1,36 +1,102 @@
1
- import { SumScoreVariableID } from "../primitives/ID";
2
- import { BuilderObject } from "../BuilderObject";
3
-
4
- export interface SumScoreVariableDto {
5
- id: SumScoreVariableID;
6
- name: string;
7
- description: string;
8
- useAvg: boolean;
9
- /**
10
- * All variables that the sum-score should be based on.
11
- */
12
- basedOn: Array<{ varId: string; varLabel: string; weight?: number }>;
13
- }
14
-
15
- export class SumScoreVariable extends BuilderObject<
16
- "builder-sum-score-variable",
17
- SumScoreVariableDto
18
- > {
19
- readonly objectType = "builder-sum-score-variable";
20
- useAvg = true;
21
- name = "";
22
- description = "";
23
- private _basedOn: Array<{ varId: string }> = [];
24
- public static readonly create = () => {
25
- const id = SumScoreVariableID.create();
26
- };
27
- private constructor(dto: SumScoreVariableDto) {
28
- super(dto);
29
- }
30
- toJson(): SumScoreVariableDto {
31
- throw new Error("Method not implemented.");
32
- }
33
- clone(): SumScoreVariableDto {
34
- throw new Error("Method not implemented.");
35
- }
36
- }
1
+ import { PageID, SumScoreVariableID } from "../primitives/ID";
2
+ import { BuilderObject } from "../BuilderObject";
3
+ import { DUtil } from "@media-quest/engine";
4
+ import { BuilderPage } from "../page/Builder-page";
5
+
6
+ export interface SumScoreVariableDto {
7
+ id: SumScoreVariableID;
8
+ name: string;
9
+ description: string;
10
+ useAvg: boolean;
11
+ }
12
+ export class SumScoreVariable extends BuilderObject<
13
+ "builder-sum-score-variable",
14
+ SumScoreVariableDto
15
+ > {
16
+ readonly objectType = "builder-sum-score-variable";
17
+ readonly id: SumScoreVariableID;
18
+ private _useAvg = true;
19
+ private _name = "";
20
+ private _description = "";
21
+ private _error = "";
22
+ private _usedIn: ReadonlyArray<BuilderPage> = [];
23
+ // private _basedOn = new Map<pageId: Page>()
24
+ get usedIn() {
25
+ return [...this._usedIn];
26
+ }
27
+
28
+ // private _basedOn: Array<{ varId: string }> = [];
29
+ public static readonly create = (data: {
30
+ name: string;
31
+ description: string;
32
+ useAvg: boolean;
33
+ }): SumScoreVariable => {
34
+ const id = SumScoreVariableID.create();
35
+ return new SumScoreVariable({ id, ...data });
36
+ };
37
+
38
+ get hasErrors() {
39
+ return this._error.length !== 0;
40
+ }
41
+
42
+ public static fromDto = (dto: SumScoreVariableDto): SumScoreVariable => {
43
+ return new SumScoreVariable(dto);
44
+ };
45
+
46
+ private constructor(dto: SumScoreVariableDto) {
47
+ super(dto);
48
+ this.id = dto.id;
49
+ this._name = dto.name;
50
+ this._useAvg = dto.useAvg;
51
+ this._description = dto.description;
52
+ }
53
+ toJson(): SumScoreVariableDto {
54
+ const dto: SumScoreVariableDto = {
55
+ description: this.description,
56
+ id: this.id,
57
+ name: this.name,
58
+ useAvg: this.useAvg,
59
+ };
60
+ return dto;
61
+ }
62
+ clone(): SumScoreVariableDto {
63
+ const id = SumScoreVariableID.create();
64
+ const dto = this.toJson();
65
+ return { ...dto, id };
66
+ }
67
+
68
+ /** @internal*/
69
+ _update(data: { name?: string; description?: string; useAvg?: boolean }) {
70
+ const d = data ?? {};
71
+ if (DUtil.isString(d.name)) {
72
+ this._name = d.name;
73
+ }
74
+ if (DUtil.isString(d.description)) {
75
+ this._description = d.description;
76
+ }
77
+ if (DUtil.isBool(d.useAvg)) {
78
+ this._useAvg = d.useAvg;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * @internal - used by sum-score-variable-collection.
84
+ */
85
+ _setError(error: "" | "Duplicate name") {
86
+ this._error = error;
87
+ }
88
+ /** @internal - used by sum-score-variable-collection */
89
+ _setUsedInPages(pages: BuilderPage[]) {
90
+ this._usedIn = [...pages];
91
+ }
92
+
93
+ get name() {
94
+ return this._name;
95
+ }
96
+ get description() {
97
+ return this._description;
98
+ }
99
+ get useAvg() {
100
+ return this._useAvg;
101
+ }
102
+ }