@media-quest/builder 0.0.25 → 0.0.26

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.
Files changed (44) hide show
  1. package/package.json +15 -15
  2. package/src/Builder-option.ts +64 -64
  3. package/src/Builder-page.spec.ts +2 -160
  4. package/src/Builder-page.ts +6 -83
  5. package/src/Builder-question.spec.ts +68 -68
  6. package/src/Builder-question.ts +102 -102
  7. package/src/Builder-schema.spec.ts +86 -114
  8. package/src/Builder-schema.ts +13 -9
  9. package/src/Builder-text.spec.ts +24 -24
  10. package/src/Builder-text.ts +57 -57
  11. package/src/BuilderObject.ts +30 -29
  12. package/src/BuilderTag.ts +96 -96
  13. package/src/builder-compiler.ts +1 -1
  14. package/src/code-book/codebook-variable.ts +29 -0
  15. package/src/{codebook.ts → code-book/codebook.ts} +72 -72
  16. package/src/primitives/ID.spec.ts +39 -39
  17. package/src/primitives/ID.ts +138 -119
  18. package/src/primitives/page-prefix.ts +59 -59
  19. package/src/primitives/varID.ts +12 -12
  20. package/src/public-api.ts +28 -26
  21. package/src/rulebuilder/Builder-rule.spec.ts +323 -323
  22. package/src/rulebuilder/Builder-rule.ts +191 -191
  23. package/src/rulebuilder/RuleBuilder-test-utils.ts +320 -320
  24. package/src/rulebuilder/RuleInput.ts +30 -30
  25. package/src/rulebuilder/RuleVariable.ts +34 -48
  26. package/src/rulebuilder/SingleSelectItem.ts +9 -8
  27. package/src/rulebuilder/condition/Builder-condition-group.ts +14 -6
  28. package/src/rulebuilder/condition/Builder-condition.spec.ts +12 -12
  29. package/src/rulebuilder/condition/Builder-condition.ts +17 -13
  30. package/src/rulebuilder/index.ts +16 -3
  31. package/src/rulebuilder/page-action-manager.ts +33 -33
  32. package/src/rulebuilder/rule2/Rule2.ts +211 -215
  33. package/src/schema-config.ts +25 -25
  34. package/src/sum-score/sum-score-answer.ts +6 -0
  35. package/src/sum-score/sum-score-manager.spec.ts +189 -0
  36. package/src/sum-score/sum-score-manager.ts +154 -0
  37. package/src/sum-score/sum-score-membership.ts +45 -0
  38. package/src/sum-score/sum-score-variable.spec.ts +151 -0
  39. package/src/sum-score/sum-score-variable.ts +36 -0
  40. package/src/{variable → sum-score}/sum-score.ts +122 -130
  41. package/tsconfig.tsbuildinfo +1 -1
  42. package/src/variable/b-variable.ts +0 -68
  43. package/src/variable/mq-variable.spec.ts +0 -147
  44. package/src/variable/sum-score-variable.ts +0 -50
@@ -1,119 +1,138 @@
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 dummy = {
90
- a: createDummyID(idName, "a"),
91
- b: createDummyID(idName, "b"),
92
- c: createDummyID(idName, "c"),
93
- d: createDummyID(idName, "d"),
94
- e: createDummyID(idName, "e"),
95
- f: createDummyID(idName, "f"),
96
- g: createDummyID(idName, "g"),
97
- h: createDummyID(idName, "h"),
98
- i: createDummyID(idName, "i"),
99
- j: createDummyID(idName, "j"),
100
- };
101
-
102
- return Object.freeze({ create, is, validateOrCreate, validateOrThrow, name, dummy });
103
- };
104
-
105
- export type SchemaID = ID<"SCHEMA">;
106
- export const SchemaID = createTypedIdSingleton("SCHEMA");
107
-
108
- export type PageID = ID<"PAGE">;
109
- export const PageID = createTypedIdSingleton("PAGE");
110
-
111
- export type TagID = ID<"TAG">;
112
- export const TagID = createTypedIdSingleton("TAG");
113
- export type OptionID = ID<"OPTION">;
114
- export const OptionID = createTypedIdSingleton("OPTION");
115
-
116
- export type TextID = ID<"TEXT">;
117
- export const TextID = createTypedIdSingleton("TEXT");
118
- export type QuestionID = ID<"QUESTION">;
119
- export const QuestionID = createTypedIdSingleton("QUESTION");
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,59 +1,59 @@
1
- import { SchemaPrefix } from "./schema-prefix";
2
-
3
- export type PagePrefixValue = string & { __PAGE_PREFIX__: true };
4
- export const createRandomPrefix = <const P extends string>(length: number): P => {
5
- const letters = "abcdefghijklmnopqrstuvyz";
6
- const all = letters + letters.toUpperCase();
7
- let result = "";
8
- for (let i = 0; i < length; i++) {
9
- const char = all.charAt(Math.floor(Math.random() * all.length));
10
- result += char;
11
- }
12
- return result as P;
13
- };
14
-
15
- export class PagePrefix {
16
- public static readonly MIN_LENGTH = 1;
17
- private static randomLen = 5;
18
- public static readonly MAX_LENGTH = 16;
19
-
20
- public static create = (): PagePrefix => {
21
- const v = createRandomPrefix<PagePrefixValue>(PagePrefix.randomLen);
22
- return new PagePrefix(v);
23
- };
24
- public static fromString = (value: string): PagePrefix | false => {
25
- if (!PagePrefix.isValid(value)) return false;
26
- return new PagePrefix(value);
27
- };
28
-
29
- public static fromStringOrThrow = (value: string): PagePrefixValue => {
30
- if (!PagePrefix.isValid(value)) throw new Error("Invalid prefix");
31
- return value;
32
- };
33
- public static castOrCreateRandom = (value: string): PagePrefix => {
34
- const v = PagePrefix.isValid(value)
35
- ? value
36
- : createRandomPrefix<PagePrefixValue>(PagePrefix.randomLen);
37
- return new PagePrefix(v);
38
- };
39
-
40
- public static isValid = (prefix: string | 999): prefix is PagePrefixValue => {
41
- if (typeof prefix !== "string") return false;
42
- if (prefix.length < SchemaPrefix.MIN_LENGTH) return false;
43
- return prefix.length <= SchemaPrefix.MAX_LENGTH;
44
- };
45
-
46
- get value(): PagePrefixValue {
47
- return this._value;
48
- }
49
-
50
- set value(value: string) {
51
- if (!PagePrefix.isValid(value)) {
52
- console.log("INVALID PREFIX", value);
53
- } else {
54
- this._value = value;
55
- }
56
- }
57
-
58
- private constructor(private _value: PagePrefixValue) {}
59
- }
1
+ import { SchemaPrefix } from "./schema-prefix";
2
+
3
+ export type PagePrefixValue = string & { __PAGE_PREFIX__: true };
4
+ export const createRandomPrefix = <const P extends string>(length: number): P => {
5
+ const letters = "abcdefghijklmnopqrstuvyz";
6
+ const all = letters + letters.toUpperCase();
7
+ let result = "";
8
+ for (let i = 0; i < length; i++) {
9
+ const char = all.charAt(Math.floor(Math.random() * all.length));
10
+ result += char;
11
+ }
12
+ return result as P;
13
+ };
14
+
15
+ export class PagePrefix {
16
+ public static readonly MIN_LENGTH = 1;
17
+ private static randomLen = 5;
18
+ public static readonly MAX_LENGTH = 16;
19
+
20
+ public static create = (): PagePrefix => {
21
+ const v = createRandomPrefix<PagePrefixValue>(PagePrefix.randomLen);
22
+ return new PagePrefix(v);
23
+ };
24
+ public static fromString = (value: string): PagePrefix | false => {
25
+ if (!PagePrefix.isValid(value)) return false;
26
+ return new PagePrefix(value);
27
+ };
28
+
29
+ public static fromStringOrThrow = (value: string): PagePrefixValue => {
30
+ if (!PagePrefix.isValid(value)) throw new Error("Invalid prefix");
31
+ return value;
32
+ };
33
+ public static castOrCreateRandom = (value: string): PagePrefix => {
34
+ const v = PagePrefix.isValid(value)
35
+ ? value
36
+ : createRandomPrefix<PagePrefixValue>(PagePrefix.randomLen);
37
+ return new PagePrefix(v);
38
+ };
39
+
40
+ public static isValid = (prefix: string | 999): prefix is PagePrefixValue => {
41
+ if (typeof prefix !== "string") return false;
42
+ if (prefix.length < SchemaPrefix.MIN_LENGTH) return false;
43
+ return prefix.length <= SchemaPrefix.MAX_LENGTH;
44
+ };
45
+
46
+ get value(): PagePrefixValue {
47
+ return this._value;
48
+ }
49
+
50
+ set value(value: string) {
51
+ if (!PagePrefix.isValid(value)) {
52
+ console.log("INVALID PREFIX", value);
53
+ } else {
54
+ this._value = value;
55
+ }
56
+ }
57
+
58
+ private constructor(private _value: PagePrefixValue) {}
59
+ }
@@ -1,12 +1,12 @@
1
- import { PagePrefixValue } from "./page-prefix";
2
-
3
- import { SchemaPrefixValue } from "./schema-prefix";
4
-
5
- export type VarID = `${SchemaPrefixValue}_${PagePrefixValue}`;
6
-
7
- export const VarID = {
8
- create: (schemaPrefix: SchemaPrefixValue, pagePrefix: PagePrefixValue): VarID => {
9
- const varId = schemaPrefix + "_" + pagePrefix;
10
- return varId as VarID;
11
- },
12
- };
1
+ import { PagePrefixValue } from "./page-prefix";
2
+
3
+ import { SchemaPrefixValue } from "./schema-prefix";
4
+
5
+ export type VarID = `${SchemaPrefixValue}_${PagePrefixValue}`;
6
+
7
+ export const VarID = {
8
+ create: (schemaPrefix: SchemaPrefixValue, pagePrefix: PagePrefixValue): VarID => {
9
+ const varId = schemaPrefix + "_" + pagePrefix;
10
+ return varId as VarID;
11
+ },
12
+ };
package/src/public-api.ts CHANGED
@@ -1,26 +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 "./codebook";
21
- export { VarID } from "./primitives/varID";
22
- export * from "./primitives/ID";
23
- export * from "./variable/b-variable";
24
- export * from "./builder-compiler";
25
- export * from "./variable/sum-score";
26
- export { SumScoreVariableDto } from "./variable/sum-score-variable";
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";