-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeSafety.ts
169 lines (155 loc) · 4.4 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
import {
allDirections,
allDoublings,
allDownResults,
allLevels,
allMadeResults,
allMovements,
allRanks,
allResults,
allStrains,
allSuits,
allWonTrickCounts,
DownResult,
Level,
MadeResult,
playedBoardRequiredFields,
Result,
StagedBoardResult,
UnkeyedBoardResult,
UnkeyedTypeSafeBoardResult,
WonTrickCount,
} from "./bridgeEnums";
import {
DirectionLetter,
Doubling,
Movement,
Rank,
Strain,
Suit,
} from "./graphql/appsync";
class BoardResultTypeUnsafe extends Error {}
const boardResultTypeSafetyProblem = (
br?: UnkeyedBoardResult,
): string | undefined => {
if (!br) {
return "board result is undefined";
}
if (br.type === "PASSED_OUT" || br.type === "NOT_BID_NOT_PLAYED") {
if (
playedBoardRequiredFields.some((key) => br[key]) ||
br.wonTrickCount === 0
) {
return `${br.type} board result has unexpected fields`;
}
}
if (br.type === "PLAYED") {
if (!allLevels.includes(br.level as Level)) {
return `unexpected level ${br.level ?? "undefined"}`;
}
if (!allWonTrickCounts.includes(br.wonTrickCount as WonTrickCount)) {
return `unexpected wonTrickCount ${br.wonTrickCount ?? "undefined"}`;
}
const remainingRequiredSettings = [
"strain",
"doubling",
"declarer",
"leadRank",
"leadSuit",
] as const;
if (remainingRequiredSettings.some((key) => !br[key])) {
return `PLAYED board is missing required settings: ${JSON.stringify(
remainingRequiredSettings.filter(
(requiredSetting) => !br[requiredSetting],
),
)}`;
}
}
return;
};
export const typeSafeUnkeyedBoardResult = (
br?: UnkeyedBoardResult,
): UnkeyedTypeSafeBoardResult => {
const problem = boardResultTypeSafetyProblem(br);
if (problem) {
throw new BoardResultTypeUnsafe(problem);
}
return br as UnkeyedTypeSafeBoardResult;
};
export const typeSafeBoardResultOrUndefined = (
br: StagedBoardResult,
): UnkeyedTypeSafeBoardResult | undefined => {
const problem = boardResultTypeSafetyProblem(br);
if (problem) {
return;
}
return br as UnkeyedTypeSafeBoardResult;
};
export const typeSafeLevel = (level?: number | "(none)"): Level => {
if (!allLevels.includes(level as Level)) {
throw new Error(`unexpected level ${level}`);
}
return level as Level;
};
export const typeSafeMadeResult = (madeResult?: number): MadeResult => {
if (!allMadeResults.includes(madeResult as MadeResult)) {
throw new Error(`unexpected madeResult ${madeResult}`);
}
return madeResult as MadeResult;
};
export const typeSafeDownResult = (downResult?: number): DownResult => {
if (!allDownResults.includes(downResult as DownResult)) {
throw new Error(`unexpected downResult ${downResult}`);
}
return downResult as DownResult;
};
export const typeSafeResult = (result?: number): Result => {
if (!allResults.includes(result as Result)) {
throw new Error(`unexpected result ${result}`);
}
return result as Result;
};
export const typeSafeStrain = (strain?: string): Strain => {
if (!allStrains.includes(strain as Strain)) {
throw new Error(`unexpected strain ${strain}`);
}
return strain as Strain;
};
export const typeSafeDoubling = (doubling?: string): Doubling => {
if (!allDoublings.includes(doubling as Doubling)) {
throw new Error(`unexpected doubling ${doubling}`);
}
return doubling as Doubling;
};
export const typeSafeDirectionLetter = (
direction?: string,
): DirectionLetter => {
if (!allDirections.includes(direction as DirectionLetter)) {
throw new Error(`unexpected direction ${direction}`);
}
return direction as DirectionLetter;
};
export const typeSafeRank = (rank?: string): Rank => {
if (!allRanks.includes(rank as Rank)) {
throw new Error(`unexpected rank ${rank}`);
}
return rank as Rank;
};
export const typeSafeSuit = (suit?: string): Suit => {
if (!allSuits.includes(suit as Suit)) {
throw new Error(`unexpected suit ${suit}`);
}
return suit as Suit;
};
export const typeSafeWonTrickCount = (wonTrickCount?: number) => {
if (!allWonTrickCounts.includes(wonTrickCount as WonTrickCount)) {
throw new Error(`unexpected wonTrickCount ${wonTrickCount}`);
}
return wonTrickCount as WonTrickCount;
};
export const typeSafeMovement = (movement?: string) => {
if (!allMovements.includes(movement as Movement)) {
throw new Error(`unexpected movement ${movement}`);
}
return movement as Movement;
};