@media-quest/builder 0.0.27 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@media-quest/builder",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "description": "Builder library for Media-quest schemas",
5
5
  "main": "dist/public-api.js",
6
6
  "types": "dist/public-api.d.js",
@@ -24,6 +24,6 @@
24
24
  "dts": true
25
25
  },
26
26
  "peerDependencies": {
27
- "@media-quest/engine": "0.0.27"
27
+ "@media-quest/engine": "0.0.28"
28
28
  }
29
29
  }
@@ -98,6 +98,8 @@ export class BuilderSchema {
98
98
  const rulesDto = dto.rules ?? [];
99
99
  const ruleInput = schema.getRuleInput();
100
100
  schema._rules = rulesDto.map((r) => BuilderRule.fromDto(r, ruleInput));
101
+
102
+ schema.updateSumScoreRelations();
101
103
  return schema;
102
104
  }
103
105
 
@@ -145,16 +147,27 @@ export class BuilderSchema {
145
147
  return variable;
146
148
  }
147
149
  sumScoreVariableAddToPage(sumScoreVariable: SumScoreVariable, page: BuilderPage, weight: number) {
148
- // this._pages.
149
- return this._pageCollection.addSumScoreVariable(sumScoreVariable, page.id, weight);
150
+ const added = this._pageCollection.addSumScoreVariable(sumScoreVariable, page.id, weight);
151
+ this.updateSumScoreRelations();
152
+ return added;
153
+ }
154
+
155
+ private updateSumScoreRelations() {
156
+ const sumScoreVariables = this._sumScoreCollection.asArray();
157
+ this._pageCollection.updateRelationShips({ sumScoreVariables });
150
158
  }
151
159
 
152
160
  sumScoreVariableUpdate(id: SumScoreVariableID, data: Partial<SumScoreVariableDto>) {
153
161
  const didUpdate = this._sumScoreCollection._updateOne(id, data);
154
- this._pageCollection.updateAllData({ sumScoreVariables: this._sumScoreCollection.asArray() });
162
+ this.updateSumScoreRelations();
155
163
  return didUpdate;
156
164
  }
157
165
 
166
+ sumScoreVariableDeleteFromPage(pageId: PageID, sumScoreVariableId: SumScoreVariableID) {
167
+ this._pageCollection.sumScoreVariableDeleteFromPage(pageId, sumScoreVariableId);
168
+ this.updateSumScoreRelations();
169
+ }
170
+
158
171
  insertPage(page: BuilderPage, atIndex: number): boolean {
159
172
  return this.insertPageAtIndex(page, atIndex);
160
173
  }
@@ -286,7 +299,7 @@ export class BuilderSchema {
286
299
  sumScoreVariableDelete(id: SumScoreVariableID) {
287
300
  const didDelete = this._sumScoreCollection._deleteVariable(id);
288
301
  const sumScoreVariables = [...this._sumScoreCollection];
289
- this._pageCollection.updateAllData({ sumScoreVariables });
302
+ this._pageCollection.updateRelationShips({ sumScoreVariables });
290
303
  this._pageCollection.sumScoreVariableDelete(id);
291
304
  return didDelete;
292
305
  }
@@ -206,4 +206,14 @@ describe("Builder page collection", () => {
206
206
  expect(beforeSize).toBe(empty.size);
207
207
  expect(result).toBe(false);
208
208
  });
209
+ test("Will always update the page-number on every page.", () => {
210
+ const p1 = empty.add("question");
211
+ const p2 = empty.add("question");
212
+ const p3 = empty.add("question");
213
+ const beforeSize = empty.size;
214
+ const result = empty.insertPage(p1, 0);
215
+ const pages = empty.pages;
216
+ expect(beforeSize).toBe(empty.size);
217
+ expect(result).toBe(false);
218
+ });
209
219
  });
@@ -99,9 +99,18 @@ export class BuilderPageCollection implements Iterable<BuilderPage> {
99
99
  return true;
100
100
  }
101
101
 
102
- updateAllData(context: { sumScoreVariables: ReadonlyArray<SumScoreVariable> }) {
102
+ updateRelationShips(context: { sumScoreVariables: ReadonlyArray<SumScoreVariable> }) {
103
+ const { sumScoreVariables } = context;
104
+
105
+ // Update all relationships in pages.
103
106
  this._all.forEach((p) => {
104
- p.sumScoreVariableUpdateData(context.sumScoreVariables);
107
+ p.updateRelationShips(context.sumScoreVariables);
108
+ });
109
+
110
+ // Set the used in pages array on every variable.
111
+ sumScoreVariables.forEach((v) => {
112
+ const usedInPages = this._all.filter((p) => p._isIncludedInSumScore(v.id));
113
+ v._setUsedInPages(usedInPages);
105
114
  });
106
115
  }
107
116
 
@@ -110,4 +119,11 @@ export class BuilderPageCollection implements Iterable<BuilderPage> {
110
119
  p.sumScoreVariableDelete(sumScoreVariableID);
111
120
  });
112
121
  }
122
+
123
+ sumScoreVariableDeleteFromPage(pageId: PageID, sumScoreVariableId: SumScoreVariableID) {
124
+ const maybePage = this.getPageById(pageId);
125
+ if (!maybePage) return false;
126
+ maybePage.sumScoreVariableDelete(sumScoreVariableId);
127
+ return true;
128
+ }
113
129
  }
@@ -4,7 +4,8 @@ import type { BuilderQuestionDto } from "../Builder-question";
4
4
  import { DUtil } from "@media-quest/engine";
5
5
  import { PagePrefix } from "../primitives/page-prefix";
6
6
  import { SchemaPrefix } from "../primitives/schema-prefix";
7
- import { OptionID, PageID, QuestionID } from "../primitives/ID";
7
+ import { OptionID, PageID, QuestionID, SumScoreVariableID } from "../primitives/ID";
8
+ import { SumScoreVariable } from "../sum-score/sum-score-variable";
8
9
 
9
10
  const U = DUtil;
10
11
  const deleteIdsFromPage = (page: BuilderPageDto) => {
@@ -160,4 +161,17 @@ describe("Builder Page", () => {
160
161
  // expect(page1.questions.length).toBe(3);
161
162
  // expect(m1).toBe(false);
162
163
  });
164
+ test("Can check if page is included in sum-score-variable", () => {
165
+ const prefix = PagePrefix.fromStringOrThrow("as1");
166
+ const page = BuilderPage.create("question", prefix);
167
+ const ss1 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
168
+ const ss2 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
169
+ const success1 = page.sumScoreVariableSet(ss1, 1);
170
+ const success2 = page.sumScoreVariableSet(ss2, 1);
171
+ expect(success1).toBe(true);
172
+ expect(success2).toBe(true);
173
+ expect(page._isIncludedInSumScore(ss1.id)).toBe(true);
174
+ expect(page._isIncludedInSumScore(ss2.id)).toBe(true);
175
+ expect(page._isIncludedInSumScore(SumScoreVariableID.dummy.a)).toBe(false);
176
+ });
163
177
  });
@@ -160,6 +160,7 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
160
160
  this._prefix.value = value;
161
161
  }
162
162
 
163
+ /** @internal */
163
164
  sumScoreVariableSet(sumScoreVariable: SumScoreVariable, weight: number) {
164
165
  const { id, name, description } = sumScoreVariable;
165
166
 
@@ -173,7 +174,8 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
173
174
  return true;
174
175
  }
175
176
 
176
- sumScoreVariableUpdateData(variables: ReadonlyArray<SumScoreVariable>) {
177
+ /** @internal */
178
+ updateRelationShips(variables: ReadonlyArray<SumScoreVariable>) {
177
179
  variables.forEach((v) => {
178
180
  const sumScoreEntry = this._includedInSumScores.get(v.id);
179
181
  if (sumScoreEntry) {
@@ -232,6 +234,16 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
232
234
  }
233
235
  }
234
236
 
237
+ /**
238
+ * @internal
239
+ */
240
+ _isIncludedInSumScore(sumScoreId: SumScoreVariableID) {
241
+ return this._includedInSumScores.has(sumScoreId);
242
+ }
243
+
244
+ /**
245
+ * @internal
246
+ */
235
247
  sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
236
248
  this._includedInSumScores.delete(sumScoreVariableID);
237
249
  }
package/src/public-api.ts CHANGED
@@ -23,6 +23,6 @@ export * from "./primitives/ID";
23
23
  export * from "./code-book/codebook-variable";
24
24
  export * from "./builder-compiler";
25
25
  export * from "./sum-score/sum-score";
26
- export { SumScoreVariableDto } from "./sum-score/sum-score-variable";
26
+ export { SumScoreVariableDto, SumScoreVariable } from "./sum-score/sum-score-variable";
27
27
  export { SumScoreAnswer } from "./sum-score/sum-score-answer";
28
28
  export { TagCollection } from "./tag/Tag-Collection";
@@ -160,6 +160,61 @@ describe("Sum score sum-score.", () => {
160
160
  basedOn.length,
161
161
  );
162
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
+
163
218
  test("calculateAll", () => {
164
219
  const prefix = SchemaPrefix.fromValueOrThrow("angst");
165
220
  const schema = BuilderSchema.create(SchemaID.create(), "testing", prefix.value);
@@ -1,18 +1,14 @@
1
- import { SumScoreVariableID } from "../primitives/ID";
1
+ import { PageID, SumScoreVariableID } from "../primitives/ID";
2
2
  import { BuilderObject } from "../BuilderObject";
3
3
  import { DUtil } from "@media-quest/engine";
4
+ import { BuilderPage } from "../page/Builder-page";
4
5
 
5
6
  export interface SumScoreVariableDto {
6
7
  id: SumScoreVariableID;
7
8
  name: string;
8
9
  description: string;
9
10
  useAvg: boolean;
10
- /**
11
- * All variables that the sum-score should be based on.
12
- */
13
- // basedOn: Array<{ varId: string; varLabel: string; weight?: number }>;
14
11
  }
15
-
16
12
  export class SumScoreVariable extends BuilderObject<
17
13
  "builder-sum-score-variable",
18
14
  SumScoreVariableDto
@@ -23,7 +19,11 @@ export class SumScoreVariable extends BuilderObject<
23
19
  private _name = "";
24
20
  private _description = "";
25
21
  private _error = "";
22
+ private _usedIn: ReadonlyArray<BuilderPage> = [];
26
23
  // private _basedOn = new Map<pageId: Page>()
24
+ get usedIn() {
25
+ return [...this._usedIn];
26
+ }
27
27
 
28
28
  // private _basedOn: Array<{ varId: string }> = [];
29
29
  public static readonly create = (data: {
@@ -85,6 +85,10 @@ export class SumScoreVariable extends BuilderObject<
85
85
  _setError(error: "" | "Duplicate name") {
86
86
  this._error = error;
87
87
  }
88
+ /** @internal - used by sum-score-variable-collection */
89
+ _setUsedInPages(pages: BuilderPage[]) {
90
+ this._usedIn = [...pages];
91
+ }
88
92
 
89
93
  get name() {
90
94
  return this._name;