Skip to content

Commit 8514f3e

Browse files
committed
fix: 🐛 correct random string generation
1 parent 160ec09 commit 8514f3e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/random/Random.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RandomJson} from '@jsonjoy.com/json-random';
1+
import {RandomJson, randomString} from '@jsonjoy.com/json-random';
22
import {cloneBinary} from '@jsonjoy.com/util/lib/json-clone';
33
import {of} from 'rxjs';
44
import 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
}

0 commit comments

Comments
 (0)