@@ -15,7 +15,61 @@ import { string, unicodeString } from './StringArbitrary';
15
15
import { tuple } from './TupleArbitrary' ;
16
16
import { bigInt } from './BigIntArbitrary' ;
17
17
18
- export class ObjectConstraints {
18
+ /**
19
+ * Constraints for `fc.anything` and `fc.object`
20
+ */
21
+ export type ObjectConstraints = {
22
+ /** Maximal depth allowed */
23
+ maxDepth ?: number ;
24
+ /** Maximal number of keys */
25
+ maxKeys ?: number ;
26
+ /**
27
+ * Arbitrary for keys
28
+ *
29
+ * Default for `key` is: `fc.string()`
30
+ */
31
+ key ?: Arbitrary < string > ;
32
+ /**
33
+ * Arbitrary for values
34
+ *
35
+ * Default for `values` are:
36
+ * - `fc.boolean()`,
37
+ * - `fc.integer()`,
38
+ * - `fc.double()`,
39
+ * - `fc.string()`
40
+ * - constants among:
41
+ * - `null`,
42
+ * - `undefined`,
43
+ * - `Number.NaN`,
44
+ * - `+0`,
45
+ * - `-0`,
46
+ * - `Number.EPSILON`,
47
+ * - `Number.MIN_VALUE`,
48
+ * - `Number.MAX_VALUE`,
49
+ * - `Number.MIN_SAFE_INTEGER`,
50
+ * - `Number.MAX_SAFE_INTEGER`,
51
+ * - `Number.POSITIVE_INFINITY`,
52
+ * - `Number.NEGATIVE_INFINITY`
53
+ */
54
+ values ?: Arbitrary < unknown > [ ] ;
55
+ /** Also generate boxed versions of values */
56
+ withBoxedValues ?: boolean ;
57
+ /** Also generate Set */
58
+ withSet ?: boolean ;
59
+ /** Also generate Map */
60
+ withMap ?: boolean ;
61
+ /** Also generate string representations of object instances */
62
+ withObjectString ?: boolean ;
63
+ /** Also generate object with null prototype */
64
+ withNullPrototype ?: boolean ;
65
+ /** Also generate BigInt */
66
+ withBigInt ?: boolean ;
67
+ } ;
68
+
69
+ /**
70
+ * @internal
71
+ */
72
+ class QualifiedObjectConstraints {
19
73
constructor (
20
74
readonly key : Arbitrary < string > ,
21
75
readonly values : Arbitrary < unknown > [ ] ,
@@ -29,7 +83,7 @@ export class ObjectConstraints {
29
83
) { }
30
84
31
85
/**
32
- * Default value of ObjectConstraints.Settings. values field
86
+ * Default value of ObjectConstraints.values field
33
87
*/
34
88
static defaultValues ( ) : Arbitrary < unknown > [ ] {
35
89
return [
@@ -54,7 +108,6 @@ export class ObjectConstraints {
54
108
] ;
55
109
}
56
110
57
- /** @internal */
58
111
private static boxArbitraries ( arbs : Arbitrary < unknown > [ ] ) : Arbitrary < unknown > [ ] {
59
112
return arbs . map ( ( arb ) =>
60
113
arb . map ( ( v ) => {
@@ -75,19 +128,18 @@ export class ObjectConstraints {
75
128
) ;
76
129
}
77
130
78
- /** @internal */
79
131
private static boxArbitrariesIfNeeded ( arbs : Arbitrary < unknown > [ ] , boxEnabled : boolean ) : Arbitrary < unknown > [ ] {
80
- return boxEnabled ? ObjectConstraints . boxArbitraries ( arbs ) . concat ( arbs ) : arbs ;
132
+ return boxEnabled ? QualifiedObjectConstraints . boxArbitraries ( arbs ) . concat ( arbs ) : arbs ;
81
133
}
82
134
83
- static from ( settings ?: ObjectConstraints . Settings ) : ObjectConstraints {
135
+ static from ( settings ?: ObjectConstraints ) : QualifiedObjectConstraints {
84
136
function getOr < T > ( access : ( ) => T | undefined , value : T ) : T {
85
137
return settings != null && access ( ) != null ? access ( ) ! : value ;
86
138
}
87
- return new ObjectConstraints (
139
+ return new QualifiedObjectConstraints (
88
140
getOr ( ( ) => settings ! . key , string ( ) ) ,
89
- ObjectConstraints . boxArbitrariesIfNeeded (
90
- getOr ( ( ) => settings ! . values , ObjectConstraints . defaultValues ( ) ) ,
141
+ QualifiedObjectConstraints . boxArbitrariesIfNeeded (
142
+ getOr ( ( ) => settings ! . values , QualifiedObjectConstraints . defaultValues ( ) ) ,
91
143
getOr ( ( ) => settings ! . withBoxedValues , false )
92
144
) ,
93
145
getOr ( ( ) => settings ! . maxDepth , 2 ) ,
@@ -101,59 +153,8 @@ export class ObjectConstraints {
101
153
}
102
154
}
103
155
104
- export namespace ObjectConstraints {
105
- /** Constraints to be applied during object generation */
106
- export interface Settings {
107
- /** Maximal depth allowed */
108
- maxDepth ?: number ;
109
- /** Maximal number of keys */
110
- maxKeys ?: number ;
111
- /**
112
- * Arbitrary for keys
113
- *
114
- * Default for `key` is: `fc.string()`
115
- */
116
- key ?: Arbitrary < string > ;
117
- /**
118
- * Arbitrary for values
119
- *
120
- * Default for `values` are:
121
- * - `fc.boolean()`,
122
- * - `fc.integer()`,
123
- * - `fc.double()`,
124
- * - `fc.string()`
125
- * - constants among:
126
- * - `null`,
127
- * - `undefined`,
128
- * - `Number.NaN`,
129
- * - `+0`,
130
- * - `-0`,
131
- * - `Number.EPSILON`,
132
- * - `Number.MIN_VALUE`,
133
- * - `Number.MAX_VALUE`,
134
- * - `Number.MIN_SAFE_INTEGER`,
135
- * - `Number.MAX_SAFE_INTEGER`,
136
- * - `Number.POSITIVE_INFINITY`,
137
- * - `Number.NEGATIVE_INFINITY`
138
- */
139
- values ?: Arbitrary < unknown > [ ] ;
140
- /** Also generate boxed versions of values */
141
- withBoxedValues ?: boolean ;
142
- /** Also generate Set */
143
- withSet ?: boolean ;
144
- /** Also generate Map */
145
- withMap ?: boolean ;
146
- /** Also generate string representations of object instances */
147
- withObjectString ?: boolean ;
148
- /** Also generate object with null prototype */
149
- withNullPrototype ?: boolean ;
150
- /** Also generate BigInt */
151
- withBigInt ?: boolean ;
152
- }
153
- }
154
-
155
156
/** @internal */
156
- const anythingInternal = ( constraints : ObjectConstraints ) : Arbitrary < unknown > => {
157
+ const anythingInternal = ( constraints : QualifiedObjectConstraints ) : Arbitrary < unknown > => {
157
158
const arbKeys = constraints . withObjectString
158
159
? memo ( ( n ) =>
159
160
frequency (
@@ -212,7 +213,7 @@ const anythingInternal = (constraints: ObjectConstraints): Arbitrary<unknown> =>
212
213
} ;
213
214
214
215
/** @internal */
215
- const objectInternal = ( constraints : ObjectConstraints ) : Arbitrary < Record < string , unknown > > => {
216
+ const objectInternal = ( constraints : QualifiedObjectConstraints ) : Arbitrary < Record < string , unknown > > => {
216
217
return dictionary ( constraints . key , anythingInternal ( constraints ) ) ;
217
218
} ;
218
219
@@ -248,11 +249,11 @@ function anything(): Arbitrary<unknown>;
248
249
* // - [42,42,42]...
249
250
* ```
250
251
*
251
- * @param settings - Constraints to apply when building instances
252
+ * @param constraints - Constraints to apply when building instances
252
253
*/
253
- function anything ( settings : ObjectConstraints . Settings ) : Arbitrary < unknown > ;
254
- function anything ( settings ?: ObjectConstraints . Settings ) : Arbitrary < unknown > {
255
- return anythingInternal ( ObjectConstraints . from ( settings ) ) ;
254
+ function anything ( constraints : ObjectConstraints ) : Arbitrary < unknown > ;
255
+ function anything ( constraints ?: ObjectConstraints ) : Arbitrary < unknown > {
256
+ return anythingInternal ( QualifiedObjectConstraints . from ( constraints ) ) ;
256
257
}
257
258
258
259
/**
@@ -272,11 +273,11 @@ function object(): Arbitrary<Record<string, unknown>>;
272
273
* @example
273
274
* ```{} or {k: [{}, 1, 2]}```
274
275
*
275
- * @param settings - Constraints to apply when building instances
276
+ * @param constraints - Constraints to apply when building instances
276
277
*/
277
- function object ( settings : ObjectConstraints . Settings ) : Arbitrary < Record < string , unknown > > ;
278
- function object ( settings ?: ObjectConstraints . Settings ) : Arbitrary < Record < string , unknown > > {
279
- return objectInternal ( ObjectConstraints . from ( settings ) ) ;
278
+ function object ( constraints : ObjectConstraints ) : Arbitrary < Record < string , unknown > > ;
279
+ function object ( constraints ?: ObjectConstraints ) : Arbitrary < Record < string , unknown > > {
280
+ return objectInternal ( QualifiedObjectConstraints . from ( constraints ) ) ;
280
281
}
281
282
282
283
/** @internal */
0 commit comments