File tree Expand file tree Collapse file tree 1 file changed +10
-5
lines changed
Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Original file line number Diff line number Diff line change 1- import { RandomJson } from '@jsonjoy.com/json-random' ;
1+ import { RandomJson , randomString } from '@jsonjoy.com/json-random' ;
22import { cloneBinary } from '@jsonjoy.com/util/lib/json-clone' ;
33import { of } from 'rxjs' ;
44import type {
@@ -193,11 +193,16 @@ export class Random {
193193 }
194194
195195 public str ( type : StrType ) : string {
196- let length = Math . round ( Math . random ( ) * 10 ) ;
197196 const schema = type . getSchema ( ) ;
197+ const isAscii = schema . format === 'ascii' || schema . ascii ;
198198 const { min, max} = schema ;
199- if ( min !== undefined && length < min ) length = min + length ;
200- if ( max !== undefined && length > max ) length = max ;
201- return RandomJson . genString ( length ) ;
199+ let targetLength = Math . round ( Math . random ( ) * 10 ) ;
200+ if ( min !== undefined && targetLength < min ) targetLength = min + targetLength ;
201+ if ( max !== undefined && targetLength > max ) targetLength = max ;
202+ let str = isAscii ? randomString ( [ 'char' , 32 , 126 , targetLength ] ) : RandomJson . genString ( targetLength ) ;
203+ const length = str . length ;
204+ if ( min !== undefined && length < min ) str = str . padEnd ( min , '.' ) ;
205+ if ( max !== undefined && length > max ) str = str . slice ( 0 , max ) ;
206+ return str ;
202207 }
203208}
You can’t perform that action at this time.
0 commit comments