@@ -9,7 +9,9 @@ import {executeFunction} from './helpers/execute-function';
9
9
import { assert } from './utils/assert' ;
10
10
import { parseJSON } from './helpers/parse-json' ;
11
11
12
- const isObject = value => value && typeof value === 'object' ;
12
+ function isObject ( value : unknown ) : value is Record < string , unknown > {
13
+ return ! ! value && typeof value === 'object' ;
14
+ }
13
15
14
16
export type JSONConverterProps = JSONConfiguration & {
15
17
onJSONChange : ( ) => void ;
@@ -30,9 +32,9 @@ export type JSONConverterProps = JSONConfiguration & {
30
32
export class JSONConverter {
31
33
log = console ; // eslint-disable-line
32
34
configuration ! : JSONConfiguration ;
33
- onJSONChange = ( ) => { } ;
34
- json = null ;
35
- convertedJson = null ;
35
+ onJSONChange : ( ) => void = ( ) => { } ;
36
+ json : unknown = null ;
37
+ convertedJson : unknown = null ;
36
38
37
39
constructor ( props ) {
38
40
this . setProps ( props ) ;
@@ -56,11 +58,11 @@ export class JSONConverter {
56
58
}
57
59
}
58
60
59
- mergeConfiguration ( config ) {
61
+ mergeConfiguration ( config : JSONConfiguration ) {
60
62
this . configuration . merge ( config ) ;
61
63
}
62
64
63
- convert ( json ) {
65
+ convert ( json : unknown ) : unknown {
64
66
// Use shallow equality to ensure we only convert same json once
65
67
if ( ! json || json === this . json ) {
66
68
return this . convertedJson ;
@@ -86,14 +88,14 @@ export class JSONConverter {
86
88
}
87
89
}
88
90
89
- function convertJSON ( json , configuration ) {
91
+ function convertJSON ( json : unknown , configuration : JSONConfiguration ) {
90
92
// Fixup configuration
91
93
configuration = new JSONConfiguration ( configuration ) ;
92
94
return convertJSONRecursively ( json , '' , configuration ) ;
93
95
}
94
96
95
97
/** Converts JSON to props ("hydrating" classes, resolving enums and functions etc). */
96
- function convertJSONRecursively ( json , key , configuration ) {
98
+ function convertJSONRecursively ( json : unknown , key , configuration ) {
97
99
if ( Array . isArray ( json ) ) {
98
100
return json . map ( ( element , i ) => convertJSONRecursively ( element , String ( i ) , configuration ) ) ;
99
101
}
@@ -121,13 +123,13 @@ function convertJSONRecursively(json, key, configuration) {
121
123
}
122
124
123
125
/** Returns true if an object has a `type` field */
124
- function isClassInstance ( json , configuration ) {
126
+ function isClassInstance ( json : unknown , configuration : JSONConfiguration ) {
125
127
const { typeKey} = configuration ;
126
128
const isClass = isObject ( json ) && Boolean ( json [ typeKey ] ) ;
127
129
return isClass ;
128
130
}
129
131
130
- function convertClassInstance ( json , configuration ) {
132
+ function convertClassInstance ( json : unknown , configuration : JSONConfiguration ) {
131
133
// Extract the class type field
132
134
const { typeKey} = configuration ;
133
135
const type = json [ typeKey ] ;
@@ -142,7 +144,7 @@ function convertClassInstance(json, configuration) {
142
144
}
143
145
144
146
/** Plain JS object, embed functions. */
145
- function convertFunctionObject ( json , configuration ) {
147
+ function convertFunctionObject ( json , configuration : JSONConfiguration ) {
146
148
// Extract the target function field
147
149
const { functionKey} = configuration ;
148
150
const targetFunction = json [ functionKey ] ;
@@ -157,8 +159,10 @@ function convertFunctionObject(json, configuration) {
157
159
}
158
160
159
161
/** Plain JS object, convert each key and return. */
160
- function convertPlainObject ( json , configuration ) {
161
- assert ( isObject ( json ) ) ;
162
+ function convertPlainObject ( json : unknown , configuration : JSONConfiguration ) {
163
+ if ( ! isObject ( json ) ) {
164
+ throw new Error ( 'convertPlainObject: expected an object' ) ;
165
+ }
162
166
163
167
const result = { } ;
164
168
for ( const key in json ) {
@@ -171,7 +175,7 @@ function convertPlainObject(json, configuration) {
171
175
/** Convert one string value in an object
172
176
* @todo We could also support string syntax for hydrating other types, like regexps... But no current use case
173
177
*/
174
- function convertString ( string , key , configuration ) {
178
+ function convertString ( string , key , configuration : JSONConfiguration ) {
175
179
// Here the JSON value is supposed to be treated as a function
176
180
if ( string . startsWith ( FUNCTION_IDENTIFIER ) && configuration . convertFunction ) {
177
181
string = string . replace ( FUNCTION_IDENTIFIER , '' ) ;
0 commit comments