1
- import { int } from '../number' ;
2
- import { randomString } from '../string' ;
3
- import { clone } from '../util' ;
4
- import * as templates from './templates' ;
5
- import type {
6
- ArrayTemplate ,
7
- BooleanTemplate ,
8
- FloatTemplate ,
9
- IntegerTemplate ,
10
- LiteralTemplate ,
11
- MapTemplate ,
12
- NumberTemplate ,
13
- ObjectTemplate ,
14
- OrTemplate ,
15
- StringTemplate ,
16
- Template ,
17
- TemplateNode ,
18
- TemplateShorthand ,
19
- } from './types' ;
1
+ import { int } from "../number" ;
2
+ import { randomString } from "../string" ;
3
+ import { clone } from "../util" ;
4
+ import * as templates from "./templates" ;
5
+ import type { ArrayTemplate , BooleanTemplate , FloatTemplate , IntegerTemplate , LiteralTemplate , MapTemplate , NumberTemplate , ObjectTemplate , OrTemplate , StringTemplate , Template , TemplateNode , TemplateShorthand } from "./types" ;
20
6
21
7
export interface TemplateJsonOpts {
22
8
/**
@@ -37,10 +23,7 @@ export class TemplateJson {
37
23
protected nodes : number = 0 ;
38
24
protected maxNodes : number ;
39
25
40
- constructor (
41
- public readonly template : Template = templates . nil ,
42
- public readonly opts : TemplateJsonOpts = { } ,
43
- ) {
26
+ constructor ( public readonly template : Template = templates . nil , public readonly opts : TemplateJsonOpts = { } ) {
44
27
this . maxNodes = opts . maxNodes ?? 100 ;
45
28
}
46
29
@@ -51,31 +34,7 @@ export class TemplateJson {
51
34
protected generate ( tpl : Template ) : unknown {
52
35
this . nodes ++ ;
53
36
while ( typeof tpl === 'function' ) tpl = tpl ( ) ;
54
- if ( typeof tpl === 'string' ) {
55
- switch ( tpl ) {
56
- case 'arr' :
57
- return this . generateArray ( [ 'arr' ] ) ;
58
- case 'obj' :
59
- return this . generateObject ( [ 'obj' ] ) ;
60
- case 'map' :
61
- return this . generateMap ( [ 'map' , null ] ) ;
62
- case 'str' :
63
- return this . generateString ( [ 'str' ] ) ;
64
- case 'num' :
65
- return this . generateNumber ( [ 'num' ] ) ;
66
- case 'int' :
67
- return this . generateInteger ( [ 'int' ] ) ;
68
- case 'float' :
69
- return this . generateFloat ( [ 'float' ] ) ;
70
- case 'bool' :
71
- return this . generateBoolean ( [ 'bool' ] ) ;
72
- case 'nil' :
73
- return null ;
74
- default :
75
- throw new Error ( `Unknown template shorthand: ${ tpl } ` ) ;
76
- }
77
- }
78
- const template : TemplateNode = tpl ;
37
+ const template : TemplateNode = typeof tpl === 'string' ? [ tpl ] : tpl ;
79
38
const type = template [ 0 ] ;
80
39
switch ( type ) {
81
40
case 'arr' :
@@ -142,8 +101,9 @@ export class TemplateJson {
142
101
const [ , keyToken , valueTemplate = 'nil' , min = 0 , max = 5 ] = template ;
143
102
const length = this . minmax ( min , max ) ;
144
103
const result : Record < string , unknown > = { } ;
104
+ const token = keyToken ?? templates . tokensObjectKey ;
145
105
for ( let i = 0 ; i < length ; i ++ ) {
146
- const key = randomString ( keyToken ?? templates . tokensObjectKey ) ;
106
+ const key = randomString ( token ) ;
147
107
const value = this . generate ( valueTemplate ) ;
148
108
result [ key ] = value ;
149
109
}
@@ -155,7 +115,7 @@ export class TemplateJson {
155
115
}
156
116
157
117
protected generateNumber ( [ , min , max ] : NumberTemplate ) : number {
158
- if ( Math . random ( ) > 0 .5) return this . generateInteger ( [ 'int' , min , max ] ) ;
118
+ if ( Math . random ( ) > .5 ) return this . generateInteger ( [ 'int' , min , max ] ) ;
159
119
else return this . generateFloat ( [ 'float' , min , max ] ) ;
160
120
}
161
121
0 commit comments