@media-quest/engine 0.0.23 → 0.0.25
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 +1 -1
- package/src/engine/SchemaDto.ts +23 -24
- package/src/engine/history-que.spec.ts +67 -67
- package/src/engine/next-que.spec.ts +121 -121
- package/src/engine/page-que-ruleengine-action.ts +4 -6
- package/src/public-api.ts +26 -26
- package/src/rules/__test__/rule-engine.spec.ts +351 -348
- package/src/utils/DUtil.ts +116 -116
- package/src/utils/ID.spec.ts +0 -39
- package/src/utils/ID.ts +0 -73
package/src/utils/DUtil.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
export type RandomObjectId = string & { randomObjectId: true };
|
|
2
|
-
export namespace DUtil {
|
|
3
|
-
export const randomString = (length: number): string => {
|
|
4
|
-
const letters = "abcdefghijklmnopqrstuvxyz";
|
|
5
|
-
const uppercase = letters.toUpperCase();
|
|
6
|
-
const all = letters + uppercase;
|
|
7
|
-
const abs = Math.abs(length);
|
|
8
|
-
let result = "";
|
|
9
|
-
|
|
10
|
-
for (let i = 0; i < abs; i++) {
|
|
11
|
-
const char = all.charAt(Math.floor(Math.random() * all.length));
|
|
12
|
-
result += char;
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const randomObjectId = () => randomString(32)
|
|
18
|
-
|
|
19
|
-
export const deleteProp = <Obj, Key extends keyof Obj>(obj: Obj, key: Key): Omit<Obj, Key> => {
|
|
20
|
-
delete obj[key];
|
|
21
|
-
return obj;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const isInRange = (min: number, max: number) => {
|
|
25
|
-
return (value: number) => value >= min && value <= max;
|
|
26
|
-
};
|
|
27
|
-
export const isInfinity = (number: number) => {
|
|
28
|
-
return (
|
|
29
|
-
number === Number.POSITIVE_INFINITY ||
|
|
30
|
-
number === Number.NEGATIVE_INFINITY ||
|
|
31
|
-
number === Infinity
|
|
32
|
-
);
|
|
33
|
-
};
|
|
34
|
-
export type NonEmptyArray<T> = [T, ...T[]];
|
|
35
|
-
export const isNonEmptyArray = <T>(array: Array<T>): array is NonEmptyArray<T> => {
|
|
36
|
-
return array.length > 0;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const neverCheck = (args: never) => {
|
|
40
|
-
console.log("OOPS: This value slipped through the never-check", args);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const isString = (str: unknown): str is string => typeof str === "string";
|
|
44
|
-
|
|
45
|
-
export const hasKey = <T extends string>(
|
|
46
|
-
obj: unknown,
|
|
47
|
-
key: T,
|
|
48
|
-
): obj is Record<typeof key, unknown> => {
|
|
49
|
-
if (!isRecord(obj)) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const isRecord = (obj: unknown): obj is Record<string, unknown> => {
|
|
56
|
-
if (!obj) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (Array.isArray(obj)) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (typeof obj !== "object") {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (obj === null) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export const isBool = (obj?: boolean): obj is boolean => typeof obj === "boolean";
|
|
75
|
-
|
|
76
|
-
export const isTrue = (bool?: boolean): bool is true => bool === true;
|
|
77
|
-
|
|
78
|
-
export const isFalse = (bool?: boolean): bool is false => bool === false;
|
|
79
|
-
|
|
80
|
-
export const isDefined = (obj: unknown): boolean => {
|
|
81
|
-
const notNull = obj !== null;
|
|
82
|
-
const notUndefined = obj !== undefined;
|
|
83
|
-
return notNull && notUndefined;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export const hasKind = (obj: unknown): obj is { readonly kind: string } => {
|
|
87
|
-
if (!hasKey(obj, "kind")) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
if (typeof obj.kind !== "string") {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return obj.kind.length > 0;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export const hasValue = (obj: unknown): obj is { value: unknown } => {
|
|
97
|
-
return hasKey(obj, "value");
|
|
98
|
-
};
|
|
99
|
-
export const isNumber = (value?: number): value is number => {
|
|
100
|
-
const isNumber = typeof value === "number";
|
|
101
|
-
const notNaN = !Number.isNaN(value);
|
|
102
|
-
return isNumber && notNaN;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export const maxFn = (upperLimit: number) => {
|
|
106
|
-
return (value: number) => {
|
|
107
|
-
return Math.min(value, upperLimit);
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export const minFn = (lowerLimit: number) => {
|
|
112
|
-
return (value: number) => {
|
|
113
|
-
return Math.min(value, lowerLimit);
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
}
|
|
1
|
+
// export type RandomObjectId = string & { randomObjectId: true };
|
|
2
|
+
export namespace DUtil {
|
|
3
|
+
export const randomString = (length: number): string => {
|
|
4
|
+
const letters = "abcdefghijklmnopqrstuvxyz";
|
|
5
|
+
const uppercase = letters.toUpperCase();
|
|
6
|
+
const all = letters + uppercase;
|
|
7
|
+
const abs = Math.abs(length);
|
|
8
|
+
let result = "";
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < abs; i++) {
|
|
11
|
+
const char = all.charAt(Math.floor(Math.random() * all.length));
|
|
12
|
+
result += char;
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const randomObjectId = () => randomString(32);
|
|
18
|
+
|
|
19
|
+
export const deleteProp = <Obj, Key extends keyof Obj>(obj: Obj, key: Key): Omit<Obj, Key> => {
|
|
20
|
+
delete obj[key];
|
|
21
|
+
return obj;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const isInRange = (min: number, max: number) => {
|
|
25
|
+
return (value: number) => value >= min && value <= max;
|
|
26
|
+
};
|
|
27
|
+
export const isInfinity = (number: number) => {
|
|
28
|
+
return (
|
|
29
|
+
number === Number.POSITIVE_INFINITY ||
|
|
30
|
+
number === Number.NEGATIVE_INFINITY ||
|
|
31
|
+
number === Infinity
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
export type NonEmptyArray<T> = [T, ...T[]];
|
|
35
|
+
export const isNonEmptyArray = <T>(array: Array<T>): array is NonEmptyArray<T> => {
|
|
36
|
+
return array.length > 0;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const neverCheck = (args: never) => {
|
|
40
|
+
console.log("OOPS: This value slipped through the never-check", args);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const isString = (str: unknown): str is string => typeof str === "string";
|
|
44
|
+
|
|
45
|
+
export const hasKey = <T extends string>(
|
|
46
|
+
obj: unknown,
|
|
47
|
+
key: T,
|
|
48
|
+
): obj is Record<typeof key, unknown> => {
|
|
49
|
+
if (!isRecord(obj)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const isRecord = (obj: unknown): obj is Record<string, unknown> => {
|
|
56
|
+
if (!obj) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (Array.isArray(obj)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeof obj !== "object") {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (obj === null) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const isBool = (obj?: boolean): obj is boolean => typeof obj === "boolean";
|
|
75
|
+
|
|
76
|
+
export const isTrue = (bool?: boolean): bool is true => bool === true;
|
|
77
|
+
|
|
78
|
+
export const isFalse = (bool?: boolean): bool is false => bool === false;
|
|
79
|
+
|
|
80
|
+
export const isDefined = (obj: unknown): boolean => {
|
|
81
|
+
const notNull = obj !== null;
|
|
82
|
+
const notUndefined = obj !== undefined;
|
|
83
|
+
return notNull && notUndefined;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const hasKind = (obj: unknown): obj is { readonly kind: string } => {
|
|
87
|
+
if (!hasKey(obj, "kind")) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (typeof obj.kind !== "string") {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return obj.kind.length > 0;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const hasValue = (obj: unknown): obj is { value: unknown } => {
|
|
97
|
+
return hasKey(obj, "value");
|
|
98
|
+
};
|
|
99
|
+
export const isNumber = (value?: number): value is number => {
|
|
100
|
+
const isNumber = typeof value === "number";
|
|
101
|
+
const notNaN = !Number.isNaN(value);
|
|
102
|
+
return isNumber && notNaN;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const maxFn = (upperLimit: number) => {
|
|
106
|
+
return (value: number) => {
|
|
107
|
+
return Math.min(value, upperLimit);
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const minFn = (lowerLimit: number) => {
|
|
112
|
+
return (value: number) => {
|
|
113
|
+
return Math.min(value, lowerLimit);
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
}
|
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");
|