@@ -144,23 +144,6 @@ export function fuzzKeyof(
144
144
} ;
145
145
}
146
146
147
- export function fuzzInterface (
148
- b : t . InterfaceType < t . Props >
149
- ) : ConcreteFuzzer < unknown > {
150
- const keys = Object . getOwnPropertyNames ( b . props ) ;
151
- const vals = keys . map ( k => b . props [ k ] ) ;
152
- return {
153
- children : vals ,
154
- func : ( n , ...h ) => {
155
- const ret = Object . create ( null ) ;
156
- h . forEach ( ( v , i ) => {
157
- ret [ keys [ i ] ] = v . encode ( n + i ) ;
158
- } ) ;
159
- return ret ;
160
- } ,
161
- } ;
162
- }
163
-
164
147
export function fuzzTuple (
165
148
b : t . TupleType < t . Mixed [ ] >
166
149
) : ConcreteFuzzer < unknown [ ] > {
@@ -198,9 +181,11 @@ export function arrayFuzzer(maxLength: number = defaultMaxArrayLength) {
198
181
*/
199
182
export const fuzzArray = fuzzArrayWithMaxLength ( ) ;
200
183
201
- const fuzzPartialWithExtraCodec = (
202
- extra : t . Props = { ___0000_extra_ : t . number }
203
- ) => ( b : t . PartialType < t . Props > ) : ConcreteFuzzer < unknown > => {
184
+ export const defaultExtraProps = { ___0000_extra_ : t . number } ;
185
+
186
+ const fuzzPartialWithExtraCodec = ( extra : t . Props = defaultExtraProps ) => (
187
+ b : t . PartialType < t . Props >
188
+ ) : ConcreteFuzzer < unknown > => {
204
189
const kk = Object . getOwnPropertyNames ( b . props ) ;
205
190
const xx = Object . getOwnPropertyNames ( extra ) ;
206
191
const keys = Object . getOwnPropertyNames ( b . props ) . concat (
@@ -216,7 +201,7 @@ const fuzzPartialWithExtraCodec = (
216
201
h . forEach ( ( v , i ) => {
217
202
if ( n & ( 2 ** i ) ) {
218
203
// Only allow key indices from the original type
219
- // or added keys without indices .
204
+ // or added keys not present in original type .
220
205
if ( i < kk . length || ! kk . includes ( keys [ i ] ) ) {
221
206
ret [ keys [ i ] ] = v . encode ( n + i ) ;
222
207
}
@@ -227,7 +212,7 @@ const fuzzPartialWithExtraCodec = (
227
212
} ;
228
213
} ;
229
214
230
- export function partialFuzzer ( extra : t . Props = { ___0000_extra_ : t . number } ) {
215
+ export function partialFuzzer ( extra : t . Props = defaultExtraProps ) {
231
216
return gen ( fuzzPartialWithExtraCodec ( extra ) , 'PartialType' ) ;
232
217
}
233
218
@@ -236,6 +221,45 @@ export function partialFuzzer(extra: t.Props = { ___0000_extra_: t.number }) {
236
221
*/
237
222
export const fuzzPartial = fuzzPartialWithExtraCodec ( ) ;
238
223
224
+ const fuzzInterfaceWithExtraCodec = ( extra : t . Props = defaultExtraProps ) => (
225
+ b : t . InterfaceType < t . Props >
226
+ ) : ConcreteFuzzer < unknown > => {
227
+ const kk = Object . getOwnPropertyNames ( b . props ) ;
228
+ const xx = Object . getOwnPropertyNames ( extra ) ;
229
+ const keys = Object . getOwnPropertyNames ( b . props ) . concat (
230
+ Object . getOwnPropertyNames ( extra )
231
+ ) ;
232
+ const vals = keys . map ( ( k , i ) =>
233
+ i < kk . length ? b . props [ k ] : extra [ xx [ i - kk . length ] ]
234
+ ) ;
235
+ return {
236
+ children : vals ,
237
+ func : ( n , ...h ) => {
238
+ const ret = Object . create ( null ) ;
239
+ h . forEach ( ( v , i ) => {
240
+ if ( i < kk . length ) {
241
+ ret [ keys [ i ] ] = v . encode ( n + i ) ;
242
+ } else if ( n & ( 2 ** ( i - kk . length ) ) ) {
243
+ // Only allow added keys not present in original type.
244
+ if ( ! kk . includes ( keys [ i ] ) ) {
245
+ ret [ keys [ i ] ] = v . encode ( n + i ) ;
246
+ }
247
+ }
248
+ } ) ;
249
+ return ret ;
250
+ } ,
251
+ } ;
252
+ } ;
253
+
254
+ export function interfaceFuzzer ( extra : t . Props = defaultExtraProps ) {
255
+ return gen ( fuzzInterfaceWithExtraCodec ( extra ) , 'InterfaceType' ) ;
256
+ }
257
+
258
+ /**
259
+ * @deprecated
260
+ */
261
+ export const fuzzInterface = fuzzInterfaceWithExtraCodec ( ) ;
262
+
239
263
export function fuzzIntersection (
240
264
b : t . IntersectionType < t . Any [ ] >
241
265
) : ConcreteFuzzer < unknown > {
@@ -264,7 +288,7 @@ export const coreFuzzers = [
264
288
concrete ( fuzzVoid , 'VoidType' ) ,
265
289
concrete ( fuzzUnknown , 'UnknownType' ) ,
266
290
gen ( fuzzUnion , 'UnionType' ) ,
267
- gen ( fuzzInterface , 'InterfaceType' ) ,
291
+ interfaceFuzzer ( ) ,
268
292
partialFuzzer ( ) ,
269
293
arrayFuzzer ( ) ,
270
294
gen ( fuzzIntersection , 'IntersectionType' ) ,
0 commit comments