-
Notifications
You must be signed in to change notification settings - Fork 2
/
template-helpers.ts
129 lines (116 loc) · 3.49 KB
/
template-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { safety } from "./deps.ts";
export function contentGuard<T, K extends keyof T = keyof T>(
...requireKeysInSingleT: K[] // = [...keyof T] TODO: default this to all required keys
): [safety.TypeGuard<T>, ContentGuardIssueReporter] {
return [
safety.typeGuard<T>(...requireKeysInSingleT),
guardIssueReporter<T>(...requireKeysInSingleT),
];
}
export interface ContentGuardIssueReporter {
(o: unknown): string;
}
export function guardIssueReporter<T, K extends keyof T = keyof T>(
...requireKeysInSingleT: K[] // = [...keyof T] TODO: default this to all required keys
): ContentGuardIssueReporter {
return (o: unknown): string => {
if (!o || typeof o !== "object") {
return `object expected: ${JSON.stringify(o)}`;
}
return `${requireKeysInSingleT.join(", ")} properties expected in object: ${
JSON.stringify(o)
}`;
};
}
export type TemplateIdentity = string;
export interface TemplateIdContentGuard<
T extends TemplateIdentity = TemplateIdentity,
> {
(o: unknown, templateIdentity?: T): o is unknown;
}
export interface TemplateIdGuard<
T extends TemplateIdentity = TemplateIdentity,
> {
(o: unknown): o is T;
}
export interface TemplateIdGuardIssueReporter<
T extends TemplateIdentity = TemplateIdentity,
> {
(templateIdentity: T, content: unknown): string;
}
export interface TemplateIdContentGuardIssueReporter<
T extends TemplateIdentity = TemplateIdentity,
> {
(content: unknown, templateIdentity?: T): string;
}
export function templateIdentityGuard<
T extends TemplateIdentity = TemplateIdentity,
>(
templateIdentities: readonly string[],
contentGuards: Record<
T,
[safety.TypeGuard<unknown>, ContentGuardIssueReporter]
>,
): [
TemplateIdContentGuard<T>,
TemplateIdContentGuardIssueReporter<T>,
TemplateIdGuard<T>,
TemplateIdGuardIssueReporter<T>,
] {
return [
templateIdentityContentGuard<T>(contentGuards),
invalidTemplateIdContentReporter<T>(),
templateIdentityValueGuard<T>(templateIdentities),
invalidTemplateIdReporter<T>(templateIdentities),
];
}
export function templateIdentityContentGuard<
T extends TemplateIdentity = TemplateIdentity,
>(
contentGuards: Record<
T,
[safety.TypeGuard<unknown>, ContentGuardIssueReporter]
>,
): TemplateIdContentGuard<T> {
return (o: unknown, templateIdentity?: T): o is unknown => {
if (!templateIdentity) return false;
const found = contentGuards[templateIdentity];
if (found) {
const [guard] = found;
return guard(o);
}
return false;
};
}
export function templateIdentityValueGuard<
T extends TemplateIdentity = TemplateIdentity,
>(
templateIdentities: readonly string[],
): TemplateIdGuard<T> {
return (o: unknown): o is T => {
if (typeof o === "string") {
if (templateIdentities.find((id) => id === o)) return true;
}
return false;
};
}
export function invalidTemplateIdReporter<
T extends TemplateIdentity = TemplateIdentity,
>(
templateIdentities: readonly string[],
): TemplateIdGuardIssueReporter<T> {
return (templateIdentity: T, content: unknown): string => {
return `template ID '${templateIdentity}' invalid, expected: ${
templateIdentities.join(", ")
}`;
};
}
export function invalidTemplateIdContentReporter<
T extends TemplateIdentity = TemplateIdentity,
>(): TemplateIdContentGuardIssueReporter<T> {
return (content: unknown, templateIdentity?: T): string => {
return `unexpected content for template ${templateIdentity}: ${
JSON.stringify(content)
}`;
};
}