@media-quest/builder 0.0.26 → 0.0.27
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/public-api.d.ts +819 -0
- package/dist/public-api.js +2696 -0
- package/dist/public-api.js.map +1 -0
- package/package.json +29 -15
- package/src/Builder-question.ts +0 -4
- package/src/Builder-schema.spec.ts +79 -60
- package/src/Builder-schema.ts +60 -63
- package/src/code-book/codebook-variable.ts +27 -29
- package/src/code-book/codebook.ts +81 -72
- package/src/page/Builder-page-collection.spec.ts +209 -0
- package/src/page/Builder-page-collection.ts +113 -0
- package/src/{Builder-page.spec.ts → page/Builder-page.spec.ts} +7 -6
- package/src/{Builder-page.ts → page/Builder-page.ts} +72 -16
- package/src/primitives/ID.ts +135 -138
- package/src/public-api.ts +28 -28
- package/src/rulebuilder/RuleAction.ts +105 -106
- package/src/sum-score/sum-score-variable-collection.spec.ts +68 -0
- package/src/sum-score/sum-score-variable-collection.ts +101 -0
- package/src/sum-score/sum-score-variable.spec.ts +253 -151
- package/src/sum-score/sum-score-variable.ts +98 -36
- package/src/sum-score/sum-score.ts +161 -122
- package/src/tag/BuilderTag.ts +45 -0
- package/src/tag/Tag-Collection.ts +53 -0
- package/src/theme/default-theme-compiler.ts +358 -358
- package/tsconfig.json +19 -15
- package/src/BuilderTag.ts +0 -96
- package/src/sum-score/sum-score-manager.spec.ts +0 -189
- package/src/sum-score/sum-score-manager.ts +0 -154
- package/src/sum-score/sum-score-membership.ts +0 -45
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,122 +1,161 @@
|
|
|
1
|
-
import { SumScoreVariableDto } from "./sum-score-variable";
|
|
2
|
-
import { SumScoreVariableID } from "../primitives/ID";
|
|
3
|
-
import { SumScoreAnswer } from "./sum-score-answer";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
| { kind: "
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
};
|
|
1
|
+
import { SumScoreVariableDto } from "./sum-score-variable";
|
|
2
|
+
import { SumScoreVariableID } from "../primitives/ID";
|
|
3
|
+
import { SumScoreAnswer } from "./sum-score-answer";
|
|
4
|
+
import { CodeBook } from "../code-book/codebook";
|
|
5
|
+
import { BuilderSchemaDto } from "../Builder-schema";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* A constant Array that contains all legal
|
|
10
|
+
*/
|
|
11
|
+
const ALLOWED_VALUES = [0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
12
|
+
const isAllowedValue = (num: number) => {
|
|
13
|
+
const notNegative = num >= 0;
|
|
14
|
+
const lessThanNine = num < 9;
|
|
15
|
+
return notNegative && lessThanNine;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type BasedOnEntry =
|
|
19
|
+
| { kind: "missing"; varId: string }
|
|
20
|
+
| { kind: "invalid-variable"; varId: string; message: string }
|
|
21
|
+
| { kind: "has-value"; varId: string; value: number; weight: number; varLabel: string };
|
|
22
|
+
|
|
23
|
+
export interface SumScore {
|
|
24
|
+
sumScore: number;
|
|
25
|
+
avg: number; // Alle besvarte spørsmål som ikke er 9.
|
|
26
|
+
useAvg: boolean;
|
|
27
|
+
includedAnswerCount: number;
|
|
28
|
+
missingAnswerCount: number;
|
|
29
|
+
skippedBy9Count: number; // Antall vetikke / hopp over
|
|
30
|
+
basedOn: Array<{ varId: string; value: number; weight: number; varLabel: string }>;
|
|
31
|
+
errorMessages: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const calculateAll = (
|
|
35
|
+
schemaDto: BuilderSchemaDto,
|
|
36
|
+
answers: Array<SumScoreAnswer>,
|
|
37
|
+
): ReadonlyArray<SumScore> => {
|
|
38
|
+
const vs = schemaDto.sumScoreVariables ?? [];
|
|
39
|
+
|
|
40
|
+
const basedOnMap = new Map<SumScoreVariableID, Array<{ varId: string; weight: number }>>();
|
|
41
|
+
const codeBook = CodeBook.fromSchema(schemaDto);
|
|
42
|
+
// codeBook.pageVariables.find(v => v.pageId === )
|
|
43
|
+
const pages = schemaDto.pages;
|
|
44
|
+
// const a: Array<{pageId: PageID, sumScoreVariableId: SumScoreVariableID, weight: number, varId: string}> = []
|
|
45
|
+
|
|
46
|
+
pages.forEach((page) => {
|
|
47
|
+
page.includedInSumScores.forEach((includedInScores) => {
|
|
48
|
+
const variableId = includedInScores.sumScoreVariableId;
|
|
49
|
+
const weight = includedInScores.weight;
|
|
50
|
+
const p = codeBook.pageVariables.find((v) => v.pageId === page.id);
|
|
51
|
+
if (p) {
|
|
52
|
+
const currentBasedOnArray = basedOnMap.get(variableId) ?? [];
|
|
53
|
+
currentBasedOnArray.push({ varId: p.varId, weight });
|
|
54
|
+
basedOnMap.set(variableId, currentBasedOnArray);
|
|
55
|
+
} else {
|
|
56
|
+
// TODO - Global event-bus for errors?
|
|
57
|
+
console.error("INVALID DATA in calculate all sum-scores.");
|
|
58
|
+
// throw Error("Invalid data...");
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
// const pages = Array.isArray(schemaDto.pages) ? schemaDto.pages : [];
|
|
63
|
+
const results: ReadonlyArray<SumScore> = vs.map((v) => {
|
|
64
|
+
const basedOn = basedOnMap.get(v.id) ?? [];
|
|
65
|
+
return calculate(v, basedOn, answers);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return results;
|
|
69
|
+
};
|
|
70
|
+
const calculate = (
|
|
71
|
+
sumScoreVariable: SumScoreVariableDto,
|
|
72
|
+
basedOnVariables: Array<{ varId: string; weight: number }>,
|
|
73
|
+
answers: Array<SumScoreAnswer>,
|
|
74
|
+
): SumScore => {
|
|
75
|
+
const legalValues: Array<number> = [...ALLOWED_VALUES];
|
|
76
|
+
// CALCULATE THESE!!
|
|
77
|
+
let includedAnswerCount = 0;
|
|
78
|
+
let skippedBy9Count = 0;
|
|
79
|
+
let missingAnswerCount = 0;
|
|
80
|
+
let sumScore = 0;
|
|
81
|
+
const errorMessages: string[] = [];
|
|
82
|
+
const basedOn: SumScore["basedOn"] = [];
|
|
83
|
+
const useAvg = sumScoreVariable.useAvg;
|
|
84
|
+
|
|
85
|
+
const basedOnEntries: BasedOnEntry[] = basedOnVariables.map((scv) => {
|
|
86
|
+
const maybeAnswer = answers.find((v) => v.varId === scv.varId);
|
|
87
|
+
let result: BasedOnEntry = { kind: "missing", varId: scv.varId };
|
|
88
|
+
if (!maybeAnswer) {
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const value = maybeAnswer.value;
|
|
93
|
+
const varLabel = maybeAnswer.varLabel;
|
|
94
|
+
const weight = scv.weight ?? 1;
|
|
95
|
+
const varId = maybeAnswer.varId;
|
|
96
|
+
|
|
97
|
+
result = {
|
|
98
|
+
kind: "has-value",
|
|
99
|
+
varId,
|
|
100
|
+
weight,
|
|
101
|
+
value,
|
|
102
|
+
varLabel,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return result;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
basedOnEntries.forEach((entry) => {
|
|
109
|
+
if (entry.kind === "missing") {
|
|
110
|
+
missingAnswerCount++;
|
|
111
|
+
}
|
|
112
|
+
if (entry.kind === "has-value") {
|
|
113
|
+
const { varId, varLabel, weight, value } = { ...entry };
|
|
114
|
+
const isAllowed = isAllowedValue(value);
|
|
115
|
+
if (isAllowed) {
|
|
116
|
+
basedOn.push({ varId, weight, value, varLabel });
|
|
117
|
+
sumScore += entry.value * entry.weight;
|
|
118
|
+
includedAnswerCount++;
|
|
119
|
+
}
|
|
120
|
+
if (value === 9) {
|
|
121
|
+
skippedBy9Count++;
|
|
122
|
+
basedOn.push({ varId, weight, value, varLabel });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (entry.kind === "invalid-variable") {
|
|
126
|
+
errorMessages.push(entry.message);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const avg = sumScore / includedAnswerCount;
|
|
131
|
+
const result: SumScore = {
|
|
132
|
+
avg,
|
|
133
|
+
useAvg,
|
|
134
|
+
includedAnswerCount,
|
|
135
|
+
skippedBy9Count,
|
|
136
|
+
sumScore,
|
|
137
|
+
basedOn,
|
|
138
|
+
missingAnswerCount,
|
|
139
|
+
errorMessages,
|
|
140
|
+
};
|
|
141
|
+
// Calculate avg
|
|
142
|
+
|
|
143
|
+
return result;
|
|
144
|
+
};
|
|
145
|
+
const createVariable = (): SumScoreVariableDto => {
|
|
146
|
+
const id = SumScoreVariableID.create();
|
|
147
|
+
return {
|
|
148
|
+
id,
|
|
149
|
+
name: "",
|
|
150
|
+
useAvg: true,
|
|
151
|
+
description: "",
|
|
152
|
+
// basedOn: [],
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
export const SumScore = {
|
|
156
|
+
ALLOWED_VALUES,
|
|
157
|
+
calculate,
|
|
158
|
+
calculateAll,
|
|
159
|
+
isAllowedValue,
|
|
160
|
+
createVariable,
|
|
161
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BuilderObject } from "../BuilderObject";
|
|
2
|
+
import { TagID } from "../primitives/ID";
|
|
3
|
+
|
|
4
|
+
export interface BuilderTagDto {
|
|
5
|
+
readonly id: TagID;
|
|
6
|
+
readonly tag: string;
|
|
7
|
+
readonly description: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class BuilderTag extends BuilderObject<"builder-tag", BuilderTagDto> {
|
|
11
|
+
readonly objectType: "builder-tag" = "builder-tag";
|
|
12
|
+
readonly id: TagID;
|
|
13
|
+
tagText = "";
|
|
14
|
+
tagDescription = "";
|
|
15
|
+
public static readonly MAX_LENGTH = 20;
|
|
16
|
+
public static readonly MIN_LENGTH = 1;
|
|
17
|
+
|
|
18
|
+
public static readonly create = (tag: string, description: string = "") => {
|
|
19
|
+
const id = TagID.create();
|
|
20
|
+
const dto: BuilderTagDto = {
|
|
21
|
+
id,
|
|
22
|
+
tag,
|
|
23
|
+
description,
|
|
24
|
+
};
|
|
25
|
+
return new BuilderTag(dto);
|
|
26
|
+
};
|
|
27
|
+
public static readonly fromDto = (dto: BuilderTagDto) => {
|
|
28
|
+
return new BuilderTag(dto);
|
|
29
|
+
};
|
|
30
|
+
protected constructor(dto: BuilderTagDto) {
|
|
31
|
+
const id = TagID.validateOrCreate(dto.id);
|
|
32
|
+
const withId = { ...dto, id };
|
|
33
|
+
super(withId);
|
|
34
|
+
this.id = id;
|
|
35
|
+
this.tagText = dto.tag ?? "";
|
|
36
|
+
this.tagDescription = dto.description ?? "";
|
|
37
|
+
}
|
|
38
|
+
clone(): BuilderTagDto {
|
|
39
|
+
return this.toJson();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
toJson(): BuilderTagDto {
|
|
43
|
+
return { tag: this.tagText, description: this.tagDescription, id: this.id };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BuilderTag, BuilderTagDto } from "./BuilderTag";
|
|
2
|
+
|
|
3
|
+
export class TagCollection implements Iterable<BuilderTag> {
|
|
4
|
+
private readonly _tags = new Set<BuilderTag>();
|
|
5
|
+
public static readonly create = () => {
|
|
6
|
+
return new TagCollection([]);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
[Symbol.iterator]() {
|
|
10
|
+
const list = [...this._tags];
|
|
11
|
+
return list[Symbol.iterator]();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
private constructor(initialTags: ReadonlyArray<BuilderTag>) {
|
|
15
|
+
initialTags.forEach((tag) => {
|
|
16
|
+
this._tags.add(tag);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
init(tags: ReadonlyArray<BuilderTagDto>) {
|
|
21
|
+
const dtoList: ReadonlyArray<BuilderTagDto> = Array.isArray(tags) ? tags : [];
|
|
22
|
+
const all = dtoList.map(BuilderTag.fromDto);
|
|
23
|
+
all.forEach((tag) => {
|
|
24
|
+
this._tags.add(tag);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
add(tag: BuilderTag) {
|
|
29
|
+
this._tags.add(tag);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Delete this tag from collection;
|
|
34
|
+
* @param tag
|
|
35
|
+
*/
|
|
36
|
+
delete(tag: BuilderTag) {
|
|
37
|
+
this._tags.delete(tag);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
toJson(): ReadonlyArray<BuilderTagDto> {
|
|
41
|
+
const list = [...this._tags];
|
|
42
|
+
const dtoList = list.map((t) => t.toJson());
|
|
43
|
+
return dtoList;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
deleteAll(tags: Iterable<BuilderTag>) {
|
|
47
|
+
const l = tags[Symbol.iterator]();
|
|
48
|
+
const asList = [...tags];
|
|
49
|
+
asList.forEach((t) => {
|
|
50
|
+
this.delete(t);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|