@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,21 +1,22 @@
1
- import type { BuilderQuestionDto, BuilderQuestionType } from "./Builder-question";
2
- import { BuilderQuestion } from "./Builder-question";
3
- import { BuilderObject } from "./BuilderObject";
4
- import type { BuilderOptionDto } from "./Builder-option";
5
- import { BuilderOption } from "./Builder-option";
6
- import type { BuilderMainVideoDto } from "./BuilderMainVideoDto";
7
- import type { BuilderMainImageDto } from "./BuilderMainImageDto";
8
- import type { BuilderMainTextDto } from "./BuilderMainText";
9
- import { BuilderMainText } from "./BuilderMainText";
10
- import { RuleVariableOption, RuleQuestionVariable } from "./rulebuilder";
1
+ import type { BuilderQuestionDto, BuilderQuestionType } from "../Builder-question";
2
+ import { BuilderQuestion } from "../Builder-question";
3
+ import { BuilderObject } from "../BuilderObject";
4
+ import type { BuilderOptionDto } from "../Builder-option";
5
+ import { BuilderOption } from "../Builder-option";
6
+ import type { BuilderMainVideoDto } from "../BuilderMainVideoDto";
7
+ import type { BuilderMainImageDto } from "../BuilderMainImageDto";
8
+ import type { BuilderMainTextDto } from "../BuilderMainText";
9
+ import { BuilderMainText } from "../BuilderMainText";
10
+ import { RuleVariableOption, RuleQuestionVariable } from "../rulebuilder";
11
11
  import { DUtil } from "@media-quest/engine";
12
- import { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
13
- import { VarID } from "./primitives/varID";
14
- import { SchemaPrefix } from "./primitives/schema-prefix";
15
- import { PageID } from "./primitives/ID";
12
+ import { PagePrefix, PagePrefixValue } from "../primitives/page-prefix";
13
+ import { VarID } from "../primitives/varID";
14
+ import { SchemaPrefix } from "../primitives/schema-prefix";
15
+ import { PageID, SumScoreVariableID } from "../primitives/ID";
16
+ import { SumScoreVariable } from "../sum-score/sum-score-variable";
16
17
 
17
18
  const U = DUtil;
18
- export type BuilderPageType = "info-page" | "question" | "multi-select" | "form";
19
+ export type BuilderPageType = "info-page" | "question";
19
20
 
20
21
  export interface BuilderPageDto {
21
22
  readonly id: PageID;
@@ -25,6 +26,7 @@ export interface BuilderPageDto {
25
26
  nextButton: BuilderOptionDto;
26
27
  defaultQuestion: BuilderQuestionDto;
27
28
  mainMedia?: BuilderMainImageDto | BuilderMainVideoDto;
29
+ includedInSumScores: Array<{ sumScoreVariableId: SumScoreVariableID; weight: number }>;
28
30
  autoplaySequence: Array<string>;
29
31
  tags: ReadonlyArray<string>;
30
32
  }
@@ -36,6 +38,16 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
36
38
  private _prefix: PagePrefix;
37
39
  private readonly _tags: Set<string>;
38
40
  private _backgroundColor = "#FFFFFF";
41
+ private _includedInSumScores: Map<
42
+ SumScoreVariableID,
43
+ {
44
+ sumScoreVariableId: SumScoreVariableID;
45
+ weight: number;
46
+ name: string;
47
+ description: string;
48
+ }
49
+ > = new Map();
50
+
39
51
  mainMedia: BuilderMainVideoDto | BuilderMainImageDto | false = false;
40
52
  defaultQuestion: BuilderQuestion;
41
53
  mainText: BuilderMainText;
@@ -60,7 +72,7 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
60
72
  nextButton: nextButtonDto,
61
73
  mainText: mainTextDto,
62
74
  prefix: _prefix,
63
- // questions: [],
75
+ includedInSumScores: [],
64
76
  tags: [],
65
77
  };
66
78
 
@@ -82,6 +94,15 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
82
94
  this.nextButton = BuilderOption.fromJson(dto.nextButton);
83
95
  this.defaultQuestion = BuilderQuestion.fromJson(dto.defaultQuestion);
84
96
  const tagList: string[] = Array.isArray(dto.tags) ? dto.tags : [];
97
+ const sumScores = Array.isArray(dto.includedInSumScores) ? dto.includedInSumScores : [];
98
+ sumScores.forEach((item) => {
99
+ this._includedInSumScores.set(item.sumScoreVariableId, {
100
+ sumScoreVariableId: item.sumScoreVariableId,
101
+ weight: item.weight,
102
+ name: "",
103
+ description: "",
104
+ });
105
+ });
85
106
  this._tags = new Set(tagList);
86
107
  if (dto.mainMedia) {
87
108
  this.mainMedia = dto.mainMedia;
@@ -99,6 +120,10 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
99
120
  this._pageType = value;
100
121
  }
101
122
 
123
+ get includedInSumScores() {
124
+ return [...this._includedInSumScores.values()];
125
+ }
126
+
102
127
  getQuestionVariables(
103
128
  modulePrefix: SchemaPrefix,
104
129
  pageNumber: number,
@@ -135,10 +160,38 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
135
160
  this._prefix.value = value;
136
161
  }
137
162
 
163
+ /** @internal */
164
+ sumScoreVariableSet(sumScoreVariable: SumScoreVariable, weight: number) {
165
+ const { id, name, description } = sumScoreVariable;
166
+
167
+ this._includedInSumScores.set(sumScoreVariable.id, {
168
+ sumScoreVariableId: id,
169
+ weight,
170
+ name,
171
+ description,
172
+ });
173
+
174
+ return true;
175
+ }
176
+
177
+ /** @internal */
178
+ updateRelationShips(variables: ReadonlyArray<SumScoreVariable>) {
179
+ variables.forEach((v) => {
180
+ const sumScoreEntry = this._includedInSumScores.get(v.id);
181
+ if (sumScoreEntry) {
182
+ this.sumScoreVariableSet(v, sumScoreEntry.weight);
183
+ }
184
+ });
185
+ }
186
+
138
187
  toJson(): BuilderPageDto {
139
188
  const mainText = this.mainText.toJson();
140
189
  const nextButton = this.nextButton.toJson();
141
190
  const mainMedia = this.mainMedia;
191
+ const includedInSumScores = this.includedInSumScores.map(({ sumScoreVariableId, weight }) => ({
192
+ sumScoreVariableId,
193
+ weight,
194
+ }));
142
195
  const dto: BuilderPageDto = {
143
196
  _type: this.pageType,
144
197
  mainText,
@@ -146,6 +199,7 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
146
199
  nextButton,
147
200
  id: this.id,
148
201
  tags: [...this.tags],
202
+ includedInSumScores,
149
203
  prefix: this._prefix.value,
150
204
  defaultQuestion: this.defaultQuestion.toJson(),
151
205
  };
@@ -179,4 +233,18 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
179
233
  this._backgroundColor = color;
180
234
  }
181
235
  }
236
+
237
+ /**
238
+ * @internal
239
+ */
240
+ _isIncludedInSumScore(sumScoreId: SumScoreVariableID) {
241
+ return this._includedInSumScores.has(sumScoreId);
242
+ }
243
+
244
+ /**
245
+ * @internal
246
+ */
247
+ sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
248
+ this._includedInSumScores.delete(sumScoreVariableID);
249
+ }
182
250
  }
@@ -1,138 +1,135 @@
1
- // The default length of an ID
2
- const ID_LENGTH = 32;
3
-
4
- // The minimum length of an ID
5
- const MIN_LENGTH = 10;
6
-
7
- export type ID<B extends string> = string & { __ID__: B };
8
-
9
- const isID = <const B extends string>(idName: B, id?: string): id is ID<B> => {
10
- if (typeof id !== "string") return false;
11
- return id.length >= MIN_LENGTH;
12
- };
13
-
14
- const createIDByName = <const B extends string>(idName: B): ID<B> => {
15
- const letters = "abcdefghijklmnopqrstuvyz";
16
- const all = letters + letters.toUpperCase();
17
- let result = "";
18
-
19
- for (let i = 0; i < ID_LENGTH; i++) {
20
- const char = all.charAt(Math.floor(Math.random() * all.length));
21
- result += char;
22
- }
23
-
24
- return result as ID<B>;
25
- };
26
- const createDummyID = <const B extends string>(idName: B, letter: string): ID<B> => {
27
- return letter.repeat(ID_LENGTH) as ID<B>;
28
- };
29
- interface Id<T extends string> {
30
- name: T;
31
- is: (id: string) => id is ID<T>;
32
- validateOrCreate: (id: string) => ID<T>;
33
- validateOrThrow: (id: string) => ID<T>;
34
- create: () => ID<T>;
35
- dummy: {
36
- a: ID<T>;
37
- b: ID<T>;
38
- c: ID<T>;
39
- d: ID<T>;
40
- e: ID<T>;
41
- f: ID<T>;
42
- g: ID<T>;
43
- h: ID<T>;
44
- i: ID<T>;
45
- j: ID<T>;
46
- };
47
- }
48
-
49
- /**
50
- * Creates a object with helper functions for a specific ID type
51
- * @param idName
52
- */
53
- export const createTypedIdSingleton = <const B extends string>(idName: B): Id<B> => {
54
- /**
55
- * Creates a new ID of the correct type
56
- */
57
- const create = (): ID<B> => createIDByName(idName);
58
-
59
- /**
60
- * Checks if the id is of the correct type
61
- * @param id
62
- */
63
- const is = (id: string): id is ID<B> => isID(idName, id);
64
-
65
- /**
66
- * Checks if the id is of the correct type, if not it throws an error
67
- * @param id
68
- */
69
- const validateOrThrow = (id: string): ID<B> => {
70
- if (!is(id)) {
71
- throw new Error(`Invalid id: ${id}`);
72
- }
73
- return id;
74
- };
75
-
76
- /**
77
- * The lowercase name of the id (SCHEMA, PAGE, etc)
78
- */
79
- const name: B = idName;
80
-
81
- /**
82
- * Ensures that the id is of the correct type, if not it creates a new one
83
- * @param id
84
- */
85
- const validateOrCreate = (id: string): ID<B> => {
86
- return is(id) ? id : create();
87
- };
88
-
89
- const a = createDummyID(idName, "a");
90
- const b = createDummyID(idName, "b");
91
- const c = createDummyID(idName, "c");
92
- const d = createDummyID(idName, "d");
93
- const e = createDummyID(idName, "e");
94
- const f = createDummyID(idName, "f");
95
- const g = createDummyID(idName, "g");
96
- const h = createDummyID(idName, "h");
97
- const i = createDummyID(idName, "i");
98
- const j = createDummyID(idName, "j");
99
- const list = [a, b, c, d, e, f, g, h, i, j];
100
-
101
- const dummy = {
102
- a,
103
- b,
104
- c,
105
- d,
106
- e,
107
- f,
108
- g,
109
- h,
110
- i,
111
- j,
112
- list,
113
- };
114
-
115
- return Object.freeze({ create, is, validateOrCreate, validateOrThrow, name, dummy });
116
- };
117
-
118
- export type SchemaID = ID<"SCHEMA">;
119
- export const SchemaID = createTypedIdSingleton("SCHEMA");
120
-
121
- export type PageID = ID<"PAGE">;
122
- export const PageID = createTypedIdSingleton("PAGE");
123
-
124
- export type TagID = ID<"TAG">;
125
- export const TagID = createTypedIdSingleton("TAG");
126
- export type OptionID = ID<"OPTION">;
127
- export const OptionID = createTypedIdSingleton("OPTION");
128
-
129
- export type TextID = ID<"TEXT">;
130
- export const TextID = createTypedIdSingleton("TEXT");
131
- export type QuestionID = ID<"QUESTION">;
132
- export const QuestionID = createTypedIdSingleton("QUESTION");
133
-
134
- export type SumScoreVariableID = ID<"SUM_SCORE_VARIABLE">;
135
- export const SumScoreVariableID = createTypedIdSingleton("SUM_SCORE_VARIABLE");
136
-
137
- export type SumScoreMemberShipID = ID<"SUM_SCORE_MEMBERSHIP">;
138
- export const SumScoreMemberShipID = createTypedIdSingleton("SUM_SCORE_MEMBERSHIP");
1
+ // The default length of an ID
2
+ const ID_LENGTH = 32;
3
+
4
+ // The minimum length of an ID
5
+ const MIN_LENGTH = 10;
6
+
7
+ export type ID<B extends string> = string & { __ID__: B };
8
+
9
+ const isID = <const B extends string>(idName: B, id?: string): id is ID<B> => {
10
+ if (typeof id !== "string") return false;
11
+ return id.length >= MIN_LENGTH;
12
+ };
13
+
14
+ const createIDByName = <const B extends string>(idName: B): ID<B> => {
15
+ const letters = "abcdefghijklmnopqrstuvyz";
16
+ const all = letters + letters.toUpperCase();
17
+ let result = "";
18
+
19
+ for (let i = 0; i < ID_LENGTH; i++) {
20
+ const char = all.charAt(Math.floor(Math.random() * all.length));
21
+ result += char;
22
+ }
23
+
24
+ return result as ID<B>;
25
+ };
26
+ const createDummyID = <const B extends string>(idName: B, letter: string): ID<B> => {
27
+ return letter.repeat(ID_LENGTH) as ID<B>;
28
+ };
29
+ interface Id<T extends string> {
30
+ name: T;
31
+ is: (id: string) => id is ID<T>;
32
+ validateOrCreate: (id: string) => ID<T>;
33
+ validateOrThrow: (id: string) => ID<T>;
34
+ create: () => ID<T>;
35
+ dummy: {
36
+ a: ID<T>;
37
+ b: ID<T>;
38
+ c: ID<T>;
39
+ d: ID<T>;
40
+ e: ID<T>;
41
+ f: ID<T>;
42
+ g: ID<T>;
43
+ h: ID<T>;
44
+ i: ID<T>;
45
+ j: ID<T>;
46
+ };
47
+ }
48
+
49
+ /**
50
+ * Creates a object with helper functions for a specific ID type
51
+ * @param idName
52
+ */
53
+ export const createTypedIdSingleton = <const B extends string>(idName: B): Id<B> => {
54
+ /**
55
+ * Creates a new ID of the correct type
56
+ */
57
+ const create = (): ID<B> => createIDByName(idName);
58
+
59
+ /**
60
+ * Checks if the id is of the correct type
61
+ * @param id
62
+ */
63
+ const is = (id: string): id is ID<B> => isID(idName, id);
64
+
65
+ /**
66
+ * Checks if the id is of the correct type, if not it throws an error
67
+ * @param id
68
+ */
69
+ const validateOrThrow = (id: string): ID<B> => {
70
+ if (!is(id)) {
71
+ throw new Error(`Invalid id: ${id}`);
72
+ }
73
+ return id;
74
+ };
75
+
76
+ /**
77
+ * The lowercase name of the id (SCHEMA, PAGE, etc)
78
+ */
79
+ const name: B = idName;
80
+
81
+ /**
82
+ * Ensures that the id is of the correct type, if not it creates a new one
83
+ * @param id
84
+ */
85
+ const validateOrCreate = (id: string): ID<B> => {
86
+ return is(id) ? id : create();
87
+ };
88
+
89
+ const a = createDummyID(idName, "a");
90
+ const b = createDummyID(idName, "b");
91
+ const c = createDummyID(idName, "c");
92
+ const d = createDummyID(idName, "d");
93
+ const e = createDummyID(idName, "e");
94
+ const f = createDummyID(idName, "f");
95
+ const g = createDummyID(idName, "g");
96
+ const h = createDummyID(idName, "h");
97
+ const i = createDummyID(idName, "i");
98
+ const j = createDummyID(idName, "j");
99
+ const list = [a, b, c, d, e, f, g, h, i, j];
100
+
101
+ const dummy = {
102
+ a,
103
+ b,
104
+ c,
105
+ d,
106
+ e,
107
+ f,
108
+ g,
109
+ h,
110
+ i,
111
+ j,
112
+ list,
113
+ };
114
+
115
+ return Object.freeze({ create, is, validateOrCreate, validateOrThrow, name, dummy });
116
+ };
117
+
118
+ export type SchemaID = ID<"SCHEMA">;
119
+ export const SchemaID = createTypedIdSingleton("SCHEMA");
120
+
121
+ export type PageID = ID<"PAGE">;
122
+ export const PageID = createTypedIdSingleton("PAGE");
123
+
124
+ export type TagID = ID<"TAG">;
125
+ export const TagID = createTypedIdSingleton("TAG");
126
+ export type OptionID = ID<"OPTION">;
127
+ export const OptionID = createTypedIdSingleton("OPTION");
128
+
129
+ export type TextID = ID<"TEXT">;
130
+ export const TextID = createTypedIdSingleton("TEXT");
131
+ export type QuestionID = ID<"QUESTION">;
132
+ export const QuestionID = createTypedIdSingleton("QUESTION");
133
+
134
+ export type SumScoreVariableID = ID<"SUM_SCORE_VARIABLE">;
135
+ export const SumScoreVariableID = createTypedIdSingleton("SUM_SCORE_VARIABLE");
package/src/public-api.ts CHANGED
@@ -1,28 +1,28 @@
1
- export { type BuilderOptionDto, BuilderOption } from "./Builder-option";
2
- export { type BuilderPageDto, type BuilderPageType, BuilderPage } from "./Builder-page";
3
- export {
4
- type BuilderQuestionDto,
5
- BuilderQuestion,
6
- type BuilderQuestionType,
7
- } from "./Builder-question";
8
- export { BuilderSchema, type BuilderSchemaDto } from "./Builder-schema";
9
- export { BuilderText, type BuilderTextDto } from "./Builder-text";
10
- export { type BuilderMainImageDto } from "./BuilderMainImageDto";
11
- export { BuilderMainText, type BuilderMainTextDto } from "./BuilderMainText";
12
- export { type BuilderMainVideoDto } from "./BuilderMainVideoDto";
13
- export { type BuilderTagDto, BuilderTag, TagCollection } from "./BuilderTag";
14
- export { type AudioFile, type ImageFile, type VideoFile } from "./media-files";
15
- // Public Api of rule-builder
16
- export * from "./rulebuilder";
17
- export { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
18
- export { SchemaPrefix, SchemaPrefixValue } from "./primitives/schema-prefix";
19
- export * from "./schema-config";
20
- export * from "./code-book/codebook";
21
- export { VarID } from "./primitives/varID";
22
- export * from "./primitives/ID";
23
- export * from "./code-book/codebook-variable";
24
- export * from "./builder-compiler";
25
- export * from "./sum-score/sum-score";
26
- export { SumScoreVariableDto } from "./sum-score/sum-score-variable";
27
- export { SumScoreAnswer } from "./sum-score/sum-score-answer";
28
- export { SumScoreManager } from "./sum-score/sum-score-manager";
1
+ export { type BuilderOptionDto, BuilderOption } from "./Builder-option";
2
+ export { type BuilderPageDto, type BuilderPageType, BuilderPage } from "./page/Builder-page";
3
+ export {
4
+ type BuilderQuestionDto,
5
+ BuilderQuestion,
6
+ type BuilderQuestionType,
7
+ } from "./Builder-question";
8
+ export { BuilderSchema, type BuilderSchemaDto } from "./Builder-schema";
9
+ export { BuilderText, type BuilderTextDto } from "./Builder-text";
10
+ export { type BuilderMainImageDto } from "./BuilderMainImageDto";
11
+ export { BuilderMainText, type BuilderMainTextDto } from "./BuilderMainText";
12
+ export { type BuilderMainVideoDto } from "./BuilderMainVideoDto";
13
+ export { type BuilderTagDto, BuilderTag } from "./tag/BuilderTag";
14
+ export { type AudioFile, type ImageFile, type VideoFile } from "./media-files";
15
+ // Public Api of rule-builder
16
+ export * from "./rulebuilder";
17
+ export { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
18
+ export { SchemaPrefix, SchemaPrefixValue } from "./primitives/schema-prefix";
19
+ export * from "./schema-config";
20
+ export * from "./code-book/codebook";
21
+ export { VarID } from "./primitives/varID";
22
+ export * from "./primitives/ID";
23
+ export * from "./code-book/codebook-variable";
24
+ export * from "./builder-compiler";
25
+ export * from "./sum-score/sum-score";
26
+ export { SumScoreVariableDto, SumScoreVariable } from "./sum-score/sum-score-variable";
27
+ export { SumScoreAnswer } from "./sum-score/sum-score-answer";
28
+ export { TagCollection } from "./tag/Tag-Collection";