@@ -8,6 +8,33 @@ export type MapData = {
8
8
starting_mode : string
9
9
}
10
10
11
+ function isRecord ( candidate : unknown ) : candidate is Record < string , unknown > {
12
+ return ! ! ( typeof candidate === 'object' && candidate ) ;
13
+ }
14
+
15
+ const mapDataSchema = {
16
+ modes : 'object' ,
17
+ min_x : 'number' ,
18
+ max_x : 'number' ,
19
+ min_y : 'number' ,
20
+ max_y : 'number' ,
21
+ starting_mode : 'string'
22
+ } as const ;
23
+
24
+ function recordFollowsMapDataSchema ( rec : Record < string , unknown > ) : rec is MapData {
25
+ for ( const key of Object . keys ( mapDataSchema ) as ( keyof typeof mapDataSchema ) [ ] ) {
26
+ if ( ! ( key in rec ) || ( typeof rec [ key ] ) !== mapDataSchema [ key ] ) {
27
+ return false ;
28
+ }
29
+
30
+ if ( mapDataSchema [ key ] === 'object' && ! isRecord ( rec [ key ] ) ) {
31
+ return false ;
32
+ }
33
+ }
34
+
35
+ return true ;
36
+ }
37
+
11
38
/**
12
39
* Checks if the given map data is valid.
13
40
*
@@ -20,23 +47,9 @@ export type MapData = {
20
47
*
21
48
* @returns Whether or not the map data is valid
22
49
*/
23
- export function isMapDataValid ( mapData : any , strict : boolean = false ) : mapData is MapData {
24
- if ( ! mapData || typeof mapData !== 'object' ) { return false ; }
25
-
26
- const schema = {
27
- modes : 'object' ,
28
- min_x : 'number' ,
29
- max_x : 'number' ,
30
- min_y : 'number' ,
31
- max_y : 'number' ,
32
- starting_mode : 'string'
33
- } as const ;
34
-
35
- for ( const key of Object . keys ( schema ) as ( keyof typeof schema ) [ ] ) {
36
- if ( ! ( key in mapData ) || ( typeof mapData [ key ] ) !== schema [ key ] ) {
37
- return false ;
38
- }
39
- }
50
+ export function isMapDataValid ( mapData : unknown , strict : boolean = false ) : mapData is MapData {
51
+ if ( ! isRecord ( mapData ) ) return false ;
52
+ if ( ! recordFollowsMapDataSchema ( mapData ) ) return false ;
40
53
41
54
if ( ! mapData . modes [ mapData . starting_mode ] ) {
42
55
return false ;
@@ -77,8 +90,9 @@ export type Mode = {
77
90
* @param mode The candidate mode to be validated.
78
91
* @returns Whether or not the mode is valid.
79
92
*/
80
- export function isModeValid ( mode : any ) : mode is Mode {
81
- if ( ! mode || typeof mode !== 'object' ) { return false ; }
93
+ // @ts -ignore | "`any` type is not allowed"
94
+ export function isModeValid ( mode : unknown ) : mode is Mode {
95
+ if ( ! isRecord ( mode ) ) { return false ; }
82
96
83
97
const schema = {
84
98
name : 'string' ,
@@ -121,19 +135,19 @@ export type Rank = typeof Rank[keyof typeof Rank];
121
135
122
136
123
137
export const modeShapeNames = ( ( ) => {
124
- let names = new Map < ModeShape , string > ( ) ;
138
+ const names = new Map < ModeShape , string > ( ) ;
125
139
126
- for ( let [ key , value ] of Object . entries ( ModeShape ) ) {
140
+ for ( const [ key , value ] of Object . entries ( ModeShape ) ) {
127
141
names . set ( value , key ) ;
128
142
}
129
143
130
144
return names as Map < ModeShape , keyof typeof ModeShape > ;
131
145
} ) ( ) ;
132
146
133
147
export const rankNames = ( ( ) => {
134
- let names = new Map < Rank , string > ( ) ;
148
+ const names = new Map < Rank , string > ( ) ;
135
149
136
- for ( let [ key , value ] of Object . entries ( Rank ) ) {
150
+ for ( const [ key , value ] of Object . entries ( Rank ) ) {
137
151
names . set ( value , key ) ;
138
152
}
139
153
0 commit comments