@media-quest/engine 0.0.24 → 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.
- package/package.json +12 -12
- package/src/engine/SchemaDto.ts +1 -2
- package/src/engine/history-que.spec.ts +2 -2
- package/src/engine/next-que.spec.ts +2 -2
- package/src/engine/page-que-ruleengine-action.ts +2 -4
- package/src/public-api.ts +1 -1
- package/src/rules/__test__/rule-engine.spec.ts +10 -7
- package/src/utils/DUtil.ts +2 -2
- package/src/utils/ID.spec.ts +0 -39
- package/src/utils/ID.ts +0 -73
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@media-quest/engine",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Rendering engine for media-quest schemas.",
|
|
5
|
-
"main": "src/public-api.ts",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rimraf dist",
|
|
9
|
-
"build": "npm run clean && tsup src/public-api.ts --sourcemap inline --format cjs,esm --dts",
|
|
10
|
-
"tsc:check:engine": "tsc --watch --noEmit"
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@media-quest/engine",
|
|
3
|
+
"version": "0.0.26",
|
|
4
|
+
"description": "Rendering engine for media-quest schemas.",
|
|
5
|
+
"main": "src/public-api.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"build": "npm run clean && tsup src/public-api.ts --sourcemap inline --format cjs,esm --dts",
|
|
10
|
+
"tsc:check:engine": "tsc --watch --noEmit"
|
|
11
|
+
}
|
|
12
|
+
}
|
package/src/engine/SchemaDto.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Rule } from "../rules/rule";
|
|
2
2
|
import { Fact } from "../rules/fact";
|
|
3
3
|
import { RuleActionPageQue } from "./page-que-ruleengine-action";
|
|
4
|
-
import { SchemaID } from "../utils/ID";
|
|
5
4
|
import { PageDto } from "../page/Page";
|
|
6
5
|
|
|
7
6
|
export type PageQueRules = Rule<RuleActionPageQue, never>;
|
|
@@ -13,7 +12,7 @@ export interface PageSequenceDto {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export interface SchemaDto {
|
|
16
|
-
readonly id:
|
|
15
|
+
readonly id: string;
|
|
17
16
|
readonly baseHeight: number;
|
|
18
17
|
readonly baseWidth: number;
|
|
19
18
|
readonly backgroundColor: string;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { HistoryQue } from "./history-que";
|
|
2
2
|
import { DTimestamp } from "../common/DTimestamp";
|
|
3
|
-
import { PageID } from "../utils/ID";
|
|
4
3
|
import { PageDto } from "../page/Page";
|
|
5
4
|
import { PageResult } from "../page/page-result";
|
|
5
|
+
import { DUtil } from "../utils/DUtil";
|
|
6
6
|
|
|
7
7
|
const p = (id: number): PageDto => {
|
|
8
8
|
return {
|
|
9
|
-
id:
|
|
9
|
+
id: DUtil.randomObjectId(),
|
|
10
10
|
prefix: "prefix" + id,
|
|
11
11
|
components: [],
|
|
12
12
|
initialTasks: [],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextQue } from "./next-que";
|
|
2
|
-
import { PageID } from "../utils/ID";
|
|
3
2
|
import { PageDto } from "../page/Page";
|
|
3
|
+
import { DUtil } from "../utils/DUtil";
|
|
4
4
|
|
|
5
5
|
const tag1 = "tag1";
|
|
6
6
|
const tag2 = "tag2";
|
|
@@ -9,7 +9,7 @@ const tag3 = "tag3";
|
|
|
9
9
|
let que = new NextQue();
|
|
10
10
|
const createPage = (tags: string[]): PageDto => {
|
|
11
11
|
const dto: PageDto = {
|
|
12
|
-
id:
|
|
12
|
+
id: DUtil.randomObjectId(),
|
|
13
13
|
prefix: "prefix",
|
|
14
14
|
staticElements: [],
|
|
15
15
|
background: "white",
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { PageID } from "../utils/ID";
|
|
2
|
-
|
|
3
1
|
export type RuleActionPageQue =
|
|
4
2
|
| { kind: "excludeByTag"; tagIds: string[] }
|
|
5
|
-
| { kind: "excludeByPageId"; pageIds: Array<
|
|
6
|
-
| { kind: "jumpToPage"; pageId:
|
|
3
|
+
| { kind: "excludeByPageId"; pageIds: Array<string> }
|
|
4
|
+
| { kind: "jumpToPage"; pageId: string };
|
package/src/public-api.ts
CHANGED
|
@@ -3,9 +3,10 @@ import { Rule } from "../rule";
|
|
|
3
3
|
import { Fact } from "../fact";
|
|
4
4
|
import { Condition } from "../condition";
|
|
5
5
|
import { RuleActionPageQue } from "../../engine/page-que-ruleengine-action";
|
|
6
|
-
import {
|
|
6
|
+
import { DUtil } from "../../utils/DUtil";
|
|
7
|
+
// import { PageID } from "../../utils/ID";
|
|
7
8
|
|
|
8
|
-
const excludeById = (ids:
|
|
9
|
+
const excludeById = (ids: string[]): RuleActionPageQue => {
|
|
9
10
|
return {
|
|
10
11
|
kind: "excludeByPageId",
|
|
11
12
|
pageIds: ids,
|
|
@@ -100,7 +101,7 @@ describe("Rule-engine spec", () => {
|
|
|
100
101
|
});
|
|
101
102
|
|
|
102
103
|
it("Empty facts => no actions, no errors.", () => {
|
|
103
|
-
const hideAs1 = excludeById([
|
|
104
|
+
const hideAs1 = excludeById([DUtil.randomObjectId()]);
|
|
104
105
|
const rule: Rule<any, any> = {
|
|
105
106
|
id: "id123",
|
|
106
107
|
description: "",
|
|
@@ -126,7 +127,7 @@ describe("Rule-engine spec", () => {
|
|
|
126
127
|
some: [],
|
|
127
128
|
all: [...trueIf_0_conditions],
|
|
128
129
|
onFailure: [],
|
|
129
|
-
onSuccess: [excludeById([
|
|
130
|
+
onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
|
|
130
131
|
};
|
|
131
132
|
const f1 = xIs(0);
|
|
132
133
|
const rules = [rule];
|
|
@@ -146,9 +147,11 @@ describe("Rule-engine spec", () => {
|
|
|
146
147
|
some: [],
|
|
147
148
|
all: [xCondition("eq", 0)],
|
|
148
149
|
onFailure: [],
|
|
149
|
-
onSuccess: [excludeById([
|
|
150
|
+
onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
|
|
150
151
|
};
|
|
152
|
+
|
|
151
153
|
expect(engine.solve(rule, facts)).toEqual(false);
|
|
154
|
+
|
|
152
155
|
const results = engine.solveAll([rule], facts);
|
|
153
156
|
expect(results.matching.length).toBe(0);
|
|
154
157
|
expect(results.errors.length).toBe(0);
|
|
@@ -162,7 +165,7 @@ describe("Rule-engine spec", () => {
|
|
|
162
165
|
some: [xCondition("eq", 6)],
|
|
163
166
|
all: [],
|
|
164
167
|
onFailure: [],
|
|
165
|
-
onSuccess: [excludeById([
|
|
168
|
+
onSuccess: [excludeById([DUtil.randomObjectId()])],
|
|
166
169
|
};
|
|
167
170
|
expect(engine.solve(rule, facts)).toBe(true);
|
|
168
171
|
const result = engine.solveAll([rule], facts);
|
|
@@ -299,7 +302,7 @@ describe("Rule-engine spec", () => {
|
|
|
299
302
|
|
|
300
303
|
it("Complex all 6 true conditions -> true", () => {
|
|
301
304
|
const facts = [xIs(0)];
|
|
302
|
-
const action = excludeById([
|
|
305
|
+
const action = excludeById([DUtil.randomObjectId()]);
|
|
303
306
|
const rule: Rule<any, any> = {
|
|
304
307
|
id: "id123",
|
|
305
308
|
|
package/src/utils/DUtil.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type RandomObjectId = string & { randomObjectId: true };
|
|
1
|
+
// export type RandomObjectId = string & { randomObjectId: true };
|
|
2
2
|
export namespace DUtil {
|
|
3
3
|
export const randomString = (length: number): string => {
|
|
4
4
|
const letters = "abcdefghijklmnopqrstuvxyz";
|
|
@@ -14,7 +14,7 @@ export namespace DUtil {
|
|
|
14
14
|
return result;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
export const randomObjectId = () => randomString(32)
|
|
17
|
+
export const randomObjectId = () => randomString(32);
|
|
18
18
|
|
|
19
19
|
export const deleteProp = <Obj, Key extends keyof Obj>(obj: Obj, key: Key): Omit<Obj, Key> => {
|
|
20
20
|
delete obj[key];
|
package/src/utils/ID.spec.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { PageID, SchemaID } from "./ID";
|
|
2
|
-
|
|
3
|
-
describe("ID Functions work", () => {
|
|
4
|
-
//Generate test for schema prefix
|
|
5
|
-
test("SCHEMA_ID isFunction works", () => {
|
|
6
|
-
const id = SchemaID.create();
|
|
7
|
-
expect(SchemaID.is(id)).toBe(true);
|
|
8
|
-
expect(SchemaID.is("")).toBe(false);
|
|
9
|
-
expect(SchemaID.is("a")).toBe(false);
|
|
10
|
-
expect(SchemaID.is("a".repeat(10))).toBe(true);
|
|
11
|
-
expect(SchemaID.is("a".repeat(9))).toBe(false);
|
|
12
|
-
});
|
|
13
|
-
test("SCHEMA_ID ensure works", () => {
|
|
14
|
-
const id = SchemaID.create();
|
|
15
|
-
expect(SchemaID.ensure(id)).toBe(id);
|
|
16
|
-
expect(SchemaID.ensure("")).not.toBe("");
|
|
17
|
-
expect(SchemaID.ensure("")).not.toBe("a");
|
|
18
|
-
expect(SchemaID.ensure("a".repeat(10))).toBe("a".repeat(10));
|
|
19
|
-
expect(SchemaID.ensure("a".repeat(9))).not.toBe("a".repeat(9));
|
|
20
|
-
expect(SchemaID.ensure("ABcdefghigKLML")).toBe("ABcdefghigKLML");
|
|
21
|
-
});
|
|
22
|
-
test("PageID isFunction works", () => {
|
|
23
|
-
const id = PageID.create();
|
|
24
|
-
expect(PageID.is(id)).toBe(true);
|
|
25
|
-
expect(PageID.is("")).toBe(false);
|
|
26
|
-
expect(PageID.is("a")).toBe(false);
|
|
27
|
-
expect(PageID.is("a".repeat(10))).toBe(true);
|
|
28
|
-
expect(PageID.is("a".repeat(9))).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
test("PageID ensure works", () => {
|
|
31
|
-
const id = PageID.create();
|
|
32
|
-
expect(PageID.ensure(id)).toBe(id);
|
|
33
|
-
expect(PageID.ensure("")).not.toBe("");
|
|
34
|
-
expect(PageID.ensure("")).not.toBe("a");
|
|
35
|
-
expect(PageID.ensure("a".repeat(10))).toBe("a".repeat(10));
|
|
36
|
-
expect(PageID.ensure("a".repeat(9))).not.toBe("a".repeat(9));
|
|
37
|
-
expect(PageID.ensure("ABcdefghigKLML")).toBe("ABcdefghigKLML");
|
|
38
|
-
});
|
|
39
|
-
});
|
package/src/utils/ID.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
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
|
-
// const SPLITTER = "_";
|
|
8
|
-
|
|
9
|
-
export type ID<B extends string> = string & { __ID__: B };
|
|
10
|
-
|
|
11
|
-
const isID = <const B extends string>(idName: B, id?: string): id is ID<B> => {
|
|
12
|
-
if (typeof id !== "string") return false;
|
|
13
|
-
return id.length >= MIN_LENGTH;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const createIDByName = <const B extends string>(idName: B): ID<B> => {
|
|
17
|
-
const letters = "abcdefghijklmnopqrstuvyz";
|
|
18
|
-
const all = letters + letters.toUpperCase();
|
|
19
|
-
let result = "";
|
|
20
|
-
for (let i = 0; i < ID_LENGTH; i++) {
|
|
21
|
-
const char = all.charAt(Math.floor(Math.random() * all.length));
|
|
22
|
-
result += char;
|
|
23
|
-
}
|
|
24
|
-
return result as ID<B>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Creates a object with helper functions for a specific ID type
|
|
29
|
-
* @param idName
|
|
30
|
-
*/
|
|
31
|
-
export const createTypedIdSingleton = <const B extends string>(idName: B) => {
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new ID of the correct type
|
|
34
|
-
*/
|
|
35
|
-
const create = (): ID<B> => createIDByName(idName);
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Checks if the id is of the correct type
|
|
39
|
-
* @param id
|
|
40
|
-
*/
|
|
41
|
-
const is = (id: string): id is ID<B> => isID(idName, id);
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Checks if the id is of the correct type, if not it throws an error
|
|
45
|
-
* @param id
|
|
46
|
-
*/
|
|
47
|
-
const validateOrThrow = (id: string): ID<B> => {
|
|
48
|
-
if (!is(id)) {
|
|
49
|
-
throw new Error(`Invalid id: ${id}`);
|
|
50
|
-
}
|
|
51
|
-
return id;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The lowercase name of the id (SCHEMA, PAGE, etc)
|
|
56
|
-
*/
|
|
57
|
-
const name: B = idName;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Ensures that the id is of the correct type, if not it creates a new one
|
|
61
|
-
* @param id
|
|
62
|
-
*/
|
|
63
|
-
const ensure = (id: string): ID<B> => {
|
|
64
|
-
return is(id) ? id : create();
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
return Object.freeze({ create, is, ensure, validateOrThrow, name });
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export type SchemaID = ID<"SCHEMA">;
|
|
71
|
-
export const SchemaID = createTypedIdSingleton("SCHEMA");
|
|
72
|
-
export type PageID = ID<"PAGE">;
|
|
73
|
-
export const PageID = createTypedIdSingleton("PAGE");
|