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" ;
206
217export interface TemplateJsonOpts {
228 /**
@@ -37,10 +23,7 @@ export class TemplateJson {
3723 protected nodes : number = 0 ;
3824 protected maxNodes : number ;
3925
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 = { } ) {
4427 this . maxNodes = opts . maxNodes ?? 100 ;
4528 }
4629
@@ -51,31 +34,7 @@ export class TemplateJson {
5134 protected generate ( tpl : Template ) : unknown {
5235 this . nodes ++ ;
5336 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 ;
7938 const type = template [ 0 ] ;
8039 switch ( type ) {
8140 case 'arr' :
@@ -142,8 +101,9 @@ export class TemplateJson {
142101 const [ , keyToken , valueTemplate = 'nil' , min = 0 , max = 5 ] = template ;
143102 const length = this . minmax ( min , max ) ;
144103 const result : Record < string , unknown > = { } ;
104+ const token = keyToken ?? templates . tokensObjectKey ;
145105 for ( let i = 0 ; i < length ; i ++ ) {
146- const key = randomString ( keyToken ?? templates . tokensObjectKey ) ;
106+ const key = randomString ( token ) ;
147107 const value = this . generate ( valueTemplate ) ;
148108 result [ key ] = value ;
149109 }
@@ -155,7 +115,7 @@ export class TemplateJson {
155115 }
156116
157117 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 ] ) ;
159119 else return this . generateFloat ( [ 'float' , min , max ] ) ;
160120 }
161121
0 commit comments