@media-quest/engine 0.0.7 → 0.0.8

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.
@@ -0,0 +1,39 @@
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
+ });
@@ -0,0 +1,71 @@
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 createID = <const B extends string>(idName: B): ID<B> => {
15
+ const letters = "abcdefghijklmnopqrstuvyz";
16
+ const all = letters + letters.toUpperCase();
17
+ let result = "";
18
+ for (let i = 0; i < ID_LENGTH; i++) {
19
+ const char = all.charAt(Math.floor(Math.random() * all.length));
20
+ result += char;
21
+ }
22
+ return result as ID<B>;
23
+ };
24
+
25
+ /**
26
+ * Creates a object with helper functions for a specific ID type
27
+ * @param idName
28
+ */
29
+ export const createTypedIdSingleton = <const B extends string>(idName: B) => {
30
+ /**
31
+ * Creates a new ID of the correct type
32
+ */
33
+ const create = (): ID<B> => createID(idName);
34
+
35
+ /**
36
+ * Checks if the id is of the correct type
37
+ * @param id
38
+ */
39
+ const is = (id: string): id is ID<B> => isID(idName, id);
40
+
41
+ /**
42
+ * Checks if the id is of the correct type, if not it throws an error
43
+ * @param id
44
+ */
45
+ const validateOrThrow = (id: string): ID<B> => {
46
+ if (!is(id)) {
47
+ throw new Error(`Invalid id: ${id}`);
48
+ }
49
+ return id;
50
+ };
51
+
52
+ /**
53
+ * The lowercase name of the id (SCHEMA, PAGE, etc)
54
+ */
55
+ const name: B = idName;
56
+
57
+ /**
58
+ * Ensures that the id is of the correct type, if not it creates a new one
59
+ * @param id
60
+ */
61
+ const ensure = (id: string): ID<B> => {
62
+ return is(id) ? id : create();
63
+ };
64
+
65
+ return Object.freeze({ create, is, ensure, validateOrThrow, name });
66
+ };
67
+
68
+ export type SchemaID = ID<"SCHEMA">;
69
+ export const SchemaID = createTypedIdSingleton("SCHEMA");
70
+ export type PageID = ID<"PAGE">;
71
+ export const PageID = createTypedIdSingleton("PAGE");