Skip to content

Commit c7b739a

Browse files
committed
🎸 feat: implement "map" template
1 parent 3b11f73 commit c7b739a

File tree

3 files changed

+20
-58
lines changed

3 files changed

+20
-58
lines changed

src/structured/TemplateJson.ts

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
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

217
export 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

src/structured/__tests__/TemplateJson.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ describe('TemplateJson', () => {
319319
});
320320

321321
test('handles complex nested templates', () => {
322-
resetMathRandom();
323-
const map = TemplateJson.gen([
322+
const map = deterministic(12345789, () => TemplateJson.gen([
324323
'map',
325324
['list', 'user_', ['pick', '1', '2', '3']],
326325
[
@@ -330,8 +329,8 @@ describe('TemplateJson', () => {
330329
['age', 'int'],
331330
],
332331
],
333-
]) as Record<string, unknown>;
334-
332+
])) as Record<string, unknown>;
333+
console.log(map);
335334
expect(typeof map).toBe('object');
336335
const keys = Object.keys(map);
337336
for (const key of keys) {

src/structured/types.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type {Token} from '../string';
33
/**
44
* Schema (template) for random JSON generation.
55
*/
6-
export type Template = TemplateShorthand | TemplateNode | TemplateRecursiveReference;
6+
export type Template =
7+
| TemplateShorthand
8+
| TemplateNode
9+
| TemplateRecursiveReference;
710

811
export type TemplateNode =
912
| LiteralTemplate
@@ -164,10 +167,10 @@ export type ObjectTemplateField = [
164167
export type MapTemplate = [
165168
type: 'map',
166169
/**
167-
* Token to use for generating the keys of the map. If `null`, the default
168-
* key {@link Token} will be used.
170+
* Token to use for generating the keys of the map. If `null` or not set,
171+
* the default key {@link Token} will be used.
169172
*/
170-
key: Token | null,
173+
key?: Token | null,
171174
/**
172175
* The template for the value of the map. If not specified, the default
173176
* template will be used.

0 commit comments

Comments
 (0)