@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,106 +1,105 @@
1
- import { PageID } from "@media-quest/engine";
2
-
3
- import { PagePrefixValue } from "../primitives/page-prefix";
4
-
5
- export interface ExcludeByPageAction {
6
- readonly kind: "exclude-by-pageId";
7
- readonly pageId: PageID;
8
- readonly pagePrefix: PagePrefixValue;
9
- readonly mainText: string;
10
- readonly pageNumber: number;
11
- }
12
-
13
- export interface JumpToPageAction {
14
- readonly kind: "jump-to-page";
15
- readonly pageId: PageID;
16
- readonly pagePrefix: PagePrefixValue;
17
- readonly mainText: string;
18
- readonly pageNumber: number;
19
- }
20
-
21
- export interface ExcludeByTagAction {
22
- readonly kind: "exclude-by-tag";
23
- readonly tag: string;
24
- readonly description: string;
25
- readonly pageCount: number;
26
- }
27
-
28
- // interface ISearchable {
29
- // getLabel(): string;
30
- // getTooltip(): string;
31
- // getSearchString(): string;
32
- // }
33
- // interface ISelectable {
34
- // isSelected: boolean;
35
- // }
36
- //
37
- // export class PageAction implements ISearchable, ISelectable {
38
- // public readonly description = "Will exclude this page from the survey.";
39
- // private _isSelected: boolean = false;
40
- // get isSelected(): boolean {
41
- // return this._isSelected;
42
- // }
43
- // set isSelected(value: boolean) {
44
- // const casted = value as unknown;
45
- // if (typeof casted === "boolean") {
46
- // this._isSelected = casted;
47
- // }
48
- // }
49
- // get pageId(): PageID {
50
- // return this.data.pageId;
51
- // }
52
- // get pagePrefix(): PagePrefixValue {
53
- // return this.data.pagePrefix;
54
- // }
55
- // get mainText(): string {
56
- // return this.data.mainText;
57
- // }
58
- // get pageNumber(): number {
59
- // return this.data.pageNumber;
60
- // }
61
- // constructor(
62
- // private readonly data: {
63
- // pageId: PageID;
64
- // pagePrefix: PagePrefixValue;
65
- // mainText: string;
66
- // pageNumber: number;
67
- // isSelected: boolean;
68
- // },
69
- // ) {
70
- // this.isSelected = this.data.isSelected;
71
- // }
72
- //
73
- // getLabel(): string {
74
- // return this.data.pagePrefix + " (" + this.data.pageNumber + ")";
75
- // }
76
- // getTooltip(): string {
77
- // return this.data.mainText;
78
- // }
79
- // getSearchString(): string {
80
- // return this.data.pagePrefix + this.data.mainText;
81
- // }
82
- // }
83
- // export class TagAction implements ISearchable {
84
- // constructor(
85
- // readonly tag: string,
86
- // readonly description: string,
87
- // readonly usedInPages: { pageNumber: number; pagePrefix: PagePrefixValue }[],
88
- // ) {}
89
- // getLabel(): string {
90
- // return this.tag + " (" + this.usedInPages.length + ")";
91
- // }
92
- // getTooltip(): string {
93
- // return this.description;
94
- // }
95
- // getSearchString(): string {
96
- // return this.tag;
97
- // }
98
- // }
99
-
100
- // class SelectableCollection<T> {
101
- // private readonly _items: Array<T>;
102
- // private selected: T | false = false;
103
- // constructor(items: ReadonlyArray<T>) {
104
- // this._items = [...items];
105
- // }
106
- // }
1
+ import { PagePrefixValue } from "../primitives/page-prefix";
2
+ import { PageID } from "../primitives/ID";
3
+
4
+ export interface ExcludeByPageAction {
5
+ readonly kind: "exclude-by-pageId";
6
+ readonly pageId: PageID;
7
+ readonly pagePrefix: PagePrefixValue;
8
+ readonly mainText: string;
9
+ readonly pageNumber: number;
10
+ }
11
+
12
+ export interface JumpToPageAction {
13
+ readonly kind: "jump-to-page";
14
+ readonly pageId: PageID;
15
+ readonly pagePrefix: PagePrefixValue;
16
+ readonly mainText: string;
17
+ readonly pageNumber: number;
18
+ }
19
+
20
+ export interface ExcludeByTagAction {
21
+ readonly kind: "exclude-by-tag";
22
+ readonly tag: string;
23
+ readonly description: string;
24
+ readonly pageCount: number;
25
+ }
26
+
27
+ // interface ISearchable {
28
+ // getLabel(): string;
29
+ // getTooltip(): string;
30
+ // getSearchString(): string;
31
+ // }
32
+ // interface ISelectable {
33
+ // isSelected: boolean;
34
+ // }
35
+ //
36
+ // export class PageAction implements ISearchable, ISelectable {
37
+ // public readonly description = "Will exclude this page from the survey.";
38
+ // private _isSelected: boolean = false;
39
+ // get isSelected(): boolean {
40
+ // return this._isSelected;
41
+ // }
42
+ // set isSelected(value: boolean) {
43
+ // const casted = value as unknown;
44
+ // if (typeof casted === "boolean") {
45
+ // this._isSelected = casted;
46
+ // }
47
+ // }
48
+ // get pageId(): PageID {
49
+ // return this.data.pageId;
50
+ // }
51
+ // get pagePrefix(): PagePrefixValue {
52
+ // return this.data.pagePrefix;
53
+ // }
54
+ // get mainText(): string {
55
+ // return this.data.mainText;
56
+ // }
57
+ // get pageNumber(): number {
58
+ // return this.data.pageNumber;
59
+ // }
60
+ // constructor(
61
+ // private readonly data: {
62
+ // pageId: PageID;
63
+ // pagePrefix: PagePrefixValue;
64
+ // mainText: string;
65
+ // pageNumber: number;
66
+ // isSelected: boolean;
67
+ // },
68
+ // ) {
69
+ // this.isSelected = this.data.isSelected;
70
+ // }
71
+ //
72
+ // getLabel(): string {
73
+ // return this.data.pagePrefix + " (" + this.data.pageNumber + ")";
74
+ // }
75
+ // getTooltip(): string {
76
+ // return this.data.mainText;
77
+ // }
78
+ // getSearchString(): string {
79
+ // return this.data.pagePrefix + this.data.mainText;
80
+ // }
81
+ // }
82
+ // export class TagAction implements ISearchable {
83
+ // constructor(
84
+ // readonly tag: string,
85
+ // readonly description: string,
86
+ // readonly usedInPages: { pageNumber: number; pagePrefix: PagePrefixValue }[],
87
+ // ) {}
88
+ // getLabel(): string {
89
+ // return this.tag + " (" + this.usedInPages.length + ")";
90
+ // }
91
+ // getTooltip(): string {
92
+ // return this.description;
93
+ // }
94
+ // getSearchString(): string {
95
+ // return this.tag;
96
+ // }
97
+ // }
98
+
99
+ // class SelectableCollection<T> {
100
+ // private readonly _items: Array<T>;
101
+ // private selected: T | false = false;
102
+ // constructor(items: ReadonlyArray<T>) {
103
+ // this._items = [...items];
104
+ // }
105
+ // }
@@ -0,0 +1,68 @@
1
+ import { SumScoreVariableCollection } from "./sum-score-variable-collection";
2
+ import { SumScoreVariable } from "./sum-score-variable";
3
+
4
+ const v = (name: string) => {
5
+ return SumScoreVariable.create({ name, description: "description for " + name, useAvg: true });
6
+ };
7
+
8
+ let empty = SumScoreVariableCollection.create([]);
9
+ beforeEach(() => {
10
+ empty = SumScoreVariableCollection.create([]);
11
+ });
12
+ describe("Sum-score-variable-collection", () => {
13
+ test("Add, update, delete works", () => {
14
+ const v1 = empty.addNew({ name: "v1", description: "v1 desc", useAvg: true });
15
+ const v2 = empty.addNew({ name: "v2", description: "v2 desc", useAvg: true });
16
+
17
+ const v2_updated = empty._updateOne(v2.id, { name: "n2", description: "d2", useAvg: false });
18
+ expect(v2_updated).toBeTruthy();
19
+ expect(empty.size).toBe(2);
20
+ const variable = empty._getVariableById(v2.id);
21
+ expect(variable).toBeDefined();
22
+ expect(variable!.name).toBe("n2");
23
+ expect(variable!.description).toBe("d2");
24
+ expect(variable!.useAvg).toBe(false);
25
+ empty._deleteVariable(v1.id);
26
+ expect(empty.size).toBe(1);
27
+ });
28
+ test("Duplicate names are detected", () => {
29
+ const v1 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
30
+ const v2 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
31
+ expect(v1.hasErrors).toBe(true);
32
+ expect(v2.hasErrors).toBe(true);
33
+ });
34
+ test("Error are removed on update", () => {
35
+ const v1 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
36
+ const v2 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
37
+ expect(v1.hasErrors).toBe(true);
38
+ expect(v2.hasErrors).toBe(true);
39
+ empty._updateOne(v1.id, { name: "updated-name-updated" });
40
+ expect(v1.hasErrors).toBe(false);
41
+ expect(v2.hasErrors).toBe(false);
42
+ });
43
+ test("Error are removed on delete", () => {
44
+ const v1 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
45
+ const v2 = empty.addNew({ name: "v1", description: "desc", useAvg: true });
46
+ expect(v1.hasErrors).toBe(true);
47
+ expect(v2.hasErrors).toBe(true);
48
+ empty._deleteVariable(v1.id);
49
+
50
+ expect(empty.size).toBe(1);
51
+ expect(v2.hasErrors).toBe(false);
52
+ });
53
+ test("Error are detected on construction", () => {
54
+ const col = SumScoreVariableCollection.create([v("v1"), v("v1")]);
55
+ expect(col.errorsCount).toBe(2);
56
+ });
57
+
58
+ test("Error are detected after init", () => {
59
+ const col = SumScoreVariableCollection.create([]);
60
+ const v1 = v("v1");
61
+ const v1_duplicate = v("v1");
62
+ const v3 = v("v3");
63
+ const v4 = v("v4");
64
+ col.init([v1, v1_duplicate, v3, v4]);
65
+ expect(col.errorsCount).toBe(2);
66
+ expect(col.size).toBe(4);
67
+ });
68
+ });
@@ -0,0 +1,101 @@
1
+ import { SumScoreVariable, SumScoreVariableDto } from "./sum-score-variable";
2
+ import { SumScoreVariableID } from "../primitives/ID";
3
+
4
+ export class SumScoreVariableCollection implements Iterable<SumScoreVariable> {
5
+ private _all: Array<SumScoreVariable> = [];
6
+
7
+ [Symbol.iterator]() {
8
+ const list = [...this._all];
9
+ return list[Symbol.iterator]();
10
+ }
11
+
12
+ asArray(): ReadonlyArray<SumScoreVariable> {
13
+ return [...this._all];
14
+ }
15
+
16
+ public static readonly create = (
17
+ variables?: SumScoreVariableDto[],
18
+ ): SumScoreVariableCollection => {
19
+ return new SumScoreVariableCollection(variables);
20
+ };
21
+
22
+ private constructor(sumScoreVariables?: SumScoreVariableDto[]) {
23
+ const all = Array.isArray(sumScoreVariables) ? sumScoreVariables : [];
24
+ this._all = all.map((dto) => SumScoreVariable.fromDto(dto));
25
+ this.checkForErrors();
26
+ }
27
+
28
+ init(variables: SumScoreVariableDto[]) {
29
+ this._all = variables.map((variable) => SumScoreVariable.fromDto(variable));
30
+ this.checkForErrors();
31
+ }
32
+ addNew(options: { name: string; description: string; useAvg: boolean }) {
33
+ // console.log(options);
34
+ const newVariable = SumScoreVariable.create({ ...options });
35
+ this._all.push(newVariable);
36
+ this.checkForErrors();
37
+ return newVariable;
38
+ }
39
+
40
+ get size() {
41
+ return this._all.length;
42
+ }
43
+
44
+ get errorsCount() {
45
+ return this._all.filter((variable) => variable.hasErrors).length;
46
+ }
47
+
48
+ toJson() {
49
+ return this._all.map((item) => item.toJson());
50
+ }
51
+
52
+ /**
53
+ *
54
+ */
55
+ _deleteVariable(id: SumScoreVariableID) {
56
+ const initialSize = this._all.length;
57
+ this._all = this._all.filter((variable) => variable.id !== id);
58
+ this.checkForErrors();
59
+ return initialSize === this.size + 1;
60
+ }
61
+
62
+ /**
63
+ * @internal
64
+ */
65
+ _updateOne(
66
+ id: SumScoreVariableID,
67
+ updates: { name?: string; description?: string; useAvg?: boolean },
68
+ ): boolean {
69
+ const variable = this._all.find((variable) => variable.id === id);
70
+ if (variable) {
71
+ variable._update(updates);
72
+ this.checkForErrors();
73
+ return true;
74
+ } else {
75
+ return false;
76
+ }
77
+ }
78
+
79
+ /** @internal */
80
+ _getVariableById(id: SumScoreVariableID): SumScoreVariable | undefined {
81
+ return this._all.find((variable) => variable.id === id);
82
+ }
83
+
84
+ private checkForErrors() {
85
+ const nameMap = new Map<string, SumScoreVariable[]>();
86
+ this._all.forEach((v) => {
87
+ const array = nameMap.get(v.name) ?? [];
88
+ array.push(v);
89
+ nameMap.set(v.name, array);
90
+ });
91
+ nameMap.forEach((sameNameArray) => {
92
+ sameNameArray.forEach((sameNameItem) => {
93
+ if (sameNameArray.length > 1) {
94
+ sameNameItem._setError("Duplicate name");
95
+ } else {
96
+ sameNameItem._setError("");
97
+ }
98
+ });
99
+ });
100
+ }
101
+ }