-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeSafety.ts
317 lines (294 loc) · 8.28 KB
/
typeSafety.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { DateTime } from "luxon";
import {
allDirections,
allDoublings,
allDownResults,
allLevels,
allMadeResults,
allMovements,
allRanks,
allResults,
allStrains,
allSuits,
allWonTrickCounts,
BoardResultU,
BoardResultUc,
BoardResultUct,
BoardResultUt,
DownResult,
Level,
MadeResult,
Result,
WonTrickCount,
} from "./bridgeEnums";
import {
DirectionLetter,
Doubling,
Movement,
Rank,
Strain,
Suit,
} from "./graphql/appsync";
class BoardResultTypeUnsafe extends Error {}
const uTypeSafetyProblemBoardResult = (
br?: BoardResultU,
): string | undefined => {
if (!br) {
return "board result is undefined";
}
if (br.type === "PASSED_OUT" || br.type === "NOT_BID_NOT_PLAYED") {
return;
}
const problems = [
tspLevel(br.level),
tspStrain(br.strain),
tspDoubling(br.doubling),
tspDirectionLetter(br.declarer),
tspRank(br.leadRank),
tspSuit(br.leadSuit),
tspWonTrickCount(br.wonTrickCount),
].filter((p) => p);
if (problems.length > 0) {
return `PLAYED board result has problems: ${problems.join(", ")}`;
}
return;
};
// const uTypeSafetyProblemTableAssignment = (
// ta?: TableAssignmentU,
// ): string | undefined => {
// if (!ta) {
// return "table assignment is undefined";
// }
// const problems = [
// tspRound(ta.round),
// tspBoolean(ta.confirmed),
// tspBoolean(ta.roundWelcomeConfirmed),
// ].filter((p) => p);
// if (problems.length > 0) {
// return `table assignment has problems: ${problems.join(", ")}`;
// }
// return;
// };
const reduceToRequiredFieldsBoardResult = (
br: BoardResultUt,
): BoardResultUt => {
if (br.type === "PASSED_OUT" || br.type === "NOT_BID_NOT_PLAYED") {
return { type: br.type };
}
return {
type: br.type,
level: br.level,
strain: br.strain,
doubling: br.doubling,
declarer: br.declarer,
leadRank: br.leadRank,
leadSuit: br.leadSuit,
wonTrickCount: br.wonTrickCount,
};
};
// const reduceToRequiredFieldsTableAssignment = (
// ta: TableAssignmentUvt,
// ): TableAssignmentUvt => {
// return {
// clubDeviceId: ta.clubDeviceId,
// confirmed: ta.confirmed,
// round: ta.round,
// roundWelcomeConfirmed: ta.roundWelcomeConfirmed,
// results: ta.results,
// playerAssignments: ta.playerAssignments,
// };
// };
export const ucToUctBoardResult = (br?: BoardResultUc): BoardResultUct => {
const problem = uTypeSafetyProblemBoardResult(br);
if (problem) {
throw new BoardResultTypeUnsafe(problem);
}
const boardResultUt = reduceToRequiredFieldsBoardResult(br as BoardResultUt);
if (!br) {
// dead code; just to satisfy the type checker that br is defined
throw new BoardResultTypeUnsafe("board result is undefined");
}
if (!DateTime.fromISO(br.currentAsOf).isValid) {
throw new BoardResultTypeUnsafe(
`currentAsOf ${br.currentAsOf} is not a valid ISO DateTime`,
);
}
return { ...boardResultUt, currentAsOf: br.currentAsOf };
};
export const uToUtBoardResult = (
br: BoardResultU,
): BoardResultUt | undefined => {
const problem = uTypeSafetyProblemBoardResult(br);
if (problem) {
return;
}
return reduceToRequiredFieldsBoardResult(br as BoardResultUt);
};
// export const uvToUvtTableAssignment = (
// ta: TableAssignmentUv,
// ): TableAssignmentUvt | undefined => {
// const problem = uTypeSafetyProblemTableAssignment(ta);
// if (problem) {
// return;
// }
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// const { clearClubDeviceId, ...taRemainder } = ta;
// return reduceToRequiredFieldsTableAssignment(
// taRemainder as TableAssignmentUvt,
// );
// };
const typeSafetyError = (problem?: string): undefined => {
if (problem) {
throw new BoardResultTypeUnsafe(problem);
}
return;
};
// tsp for type safety problem
const tspLevel = (level?: number | null): string | undefined => {
if (!allLevels.includes(level as Level)) {
return `unexpected level ${level}`;
}
return;
};
export const typeSafeLevel = (level?: number): Level => {
typeSafetyError(tspLevel(level));
return level as Level;
};
const tspMadeResult = (madeResult?: number | null): string | undefined => {
if (!allMadeResults.includes(madeResult as MadeResult)) {
return `unexpected madeResult ${madeResult}`;
}
return;
};
export const typeSafeMadeResult = (madeResult?: number): MadeResult => {
typeSafetyError(tspMadeResult(madeResult));
return madeResult as MadeResult;
};
const tspDownResult = (downResult?: number | null): string | undefined => {
if (!allDownResults.includes(downResult as DownResult)) {
return `unexpected downResult ${downResult}`;
}
return;
};
export const typeSafeDownResult = (downResult?: number): DownResult => {
typeSafetyError(tspDownResult(downResult));
return downResult as DownResult;
};
const tspResult = (result?: number | null): string | undefined => {
if (!allResults.includes(result as Result)) {
return `unexpected result ${result}`;
}
return;
};
export const typeSafeResult = (result?: number): Result => {
typeSafetyError(tspResult(result));
return result as Result;
};
const tspStrain = (strain?: string | null): string | undefined => {
if (!allStrains.includes(strain as Strain)) {
return `unexpected strain ${strain}`;
}
return;
};
export const typeSafeStrain = (strain?: string): Strain => {
typeSafetyError(tspStrain(strain));
return strain as Strain;
};
const tspDoubling = (doubling?: string | null): string | undefined => {
if (!allDoublings.includes(doubling as Doubling)) {
return `unexpected doubling ${doubling}`;
}
return;
};
export const typeSafeDoubling = (doubling?: string): Doubling => {
typeSafetyError(tspDoubling(doubling));
return doubling as Doubling;
};
const tspDirectionLetter = (direction?: string | null): string | undefined => {
if (!allDirections.includes(direction as DirectionLetter)) {
return `unexpected direction ${direction}`;
}
return;
};
export const typeSafeDirectionLetter = (
direction?: string,
): DirectionLetter => {
typeSafetyError(tspDirectionLetter(direction));
return direction as DirectionLetter;
};
const tspRank = (rank?: string | null): string | undefined => {
if (!allRanks.includes(rank as Rank)) {
return `unexpected rank ${rank}`;
}
return;
};
export const typeSafeRank = (rank?: string): Rank => {
typeSafetyError(tspRank(rank));
return rank as Rank;
};
const tspSuit = (suit?: string | null): string | undefined => {
if (!allSuits.includes(suit as Suit)) {
return `unexpected suit ${suit}`;
}
return;
};
export const typeSafeSuit = (suit?: string): Suit => {
typeSafetyError(tspSuit(suit));
return suit as Suit;
};
const tspWonTrickCount = (
wonTrickCount?: number | null,
): string | undefined => {
if (!allWonTrickCounts.includes(wonTrickCount as WonTrickCount)) {
return `unexpected wonTrickCount ${wonTrickCount}`;
}
return;
};
export const typeSafeWonTrickCount = (wonTrickCount?: number) => {
typeSafetyError(tspWonTrickCount(wonTrickCount));
return wonTrickCount as WonTrickCount;
};
const tspMovement = (movement?: string | null): string | undefined => {
if (!allMovements.includes(movement as Movement)) {
return `unexpected movement ${movement}`;
}
return;
};
export const typeSafeMovement = (movement?: string) => {
typeSafetyError(tspMovement(movement));
return movement as Movement;
};
const tspBoolean = (aBoolean?: boolean | null): string | undefined => {
if (aBoolean === undefined) {
return "boolean is undefined";
}
if (aBoolean === null) {
return "boolean is null";
}
return;
};
export const typeSafeConfirmed = (confirmed?: boolean) => {
typeSafetyError(tspBoolean(confirmed));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return confirmed!;
};
const tspRound = (round?: number | null): string | undefined => {
if (round === undefined) {
return "round is undefined";
}
if (round === null) {
return "round is null";
}
if (Math.floor(round) !== round || Math.ceil(round) !== round) {
return `round ${round} is not an integer`;
}
if (round < 0) {
return `round ${round} is negative`;
}
return;
};
export const typeSafeRound = (round?: number) => {
typeSafetyError(tspRound(round));
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return round!;
};