Skip to content

Commit d5c3d4b

Browse files
authored
Merge pull request #5 from jsonjoy-com/copilot/fix-4
feat: Organize comprehensive templates in examples.ts with helper methods and combined template
2 parents 6d5f291 + 6a4fba8 commit d5c3d4b

File tree

4 files changed

+1663
-0
lines changed

4 files changed

+1663
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,76 @@ const serviceConfig = TemplateJson.gen(['obj', [
583583
]]);
584584
```
585585

586+
## Helper Methods for Easy Generation
587+
588+
The library provides convenient helper methods for generating common data types without needing to construct templates manually. These methods are available in the `examples` module:
589+
590+
```typescript
591+
import {
592+
genUser,
593+
genAddress,
594+
genProduct,
595+
genOrder,
596+
genRandomExample
597+
} from '@jsonjoy.com/json-random/lib/examples';
598+
599+
// Generate common data types quickly
600+
const user = genUser();
601+
const address = genAddress();
602+
const product = genProduct();
603+
const order = genOrder();
604+
605+
// Generate random example from any template
606+
const randomData = genRandomExample();
607+
```
608+
609+
### Available Helper Methods
610+
611+
| Method | Description |
612+
|--------|-------------|
613+
| `genUser()` | Generate comprehensive user profile with details |
614+
| `genUserBasic()` | Generate basic user with essential information |
615+
| `genAddress()` | Generate address with street, city, state, etc. |
616+
| `genProduct()` | Generate product with name, price, category |
617+
| `genOrder()` | Generate order with items and customer info |
618+
| `genTransaction()` | Generate financial transaction data |
619+
| `genBankAccount()` | Generate bank account information |
620+
| `genSocialPost()` | Generate social media post |
621+
| `genSocialProfile()` | Generate social media profile |
622+
| `genLocation()` | Generate location with coordinates |
623+
| `genApiResponse()` | Generate API response with data array |
624+
| `genApiResponseDetailed()` | Generate comprehensive API response |
625+
| `genServiceConfig()` | Generate service configuration |
626+
| `genPatient()` | Generate medical patient record |
627+
| `genMedicalRecord()` | Generate comprehensive medical record |
628+
| `genStudent()` | Generate student profile |
629+
| `genCourse()` | Generate course information |
630+
| `genSensorReading()` | Generate IoT sensor reading |
631+
| `genIotDevice()` | Generate IoT device profile |
632+
| `genLogEntry()` | Generate log entry for monitoring |
633+
| `genMetricData()` | Generate metric data for monitoring |
634+
| `genRandomExample()` | Generate random data from any available template |
635+
636+
### Usage Examples
637+
638+
```typescript
639+
// Generate test user data for API testing
640+
const testUser = genUser();
641+
console.log(testUser);
642+
// Output: { id: 4829, username: "user_7432", email: "[email protected]", ... }
643+
644+
// Generate address for form testing
645+
const shippingAddress = genAddress();
646+
console.log(shippingAddress);
647+
// Output: { street: "123 Main St", city: "Springfield", state: "CA", ... }
648+
649+
// Generate product catalog
650+
const products = Array.from({ length: 10 }, () => genProduct());
651+
652+
// Generate random test data
653+
const randomTestData = Array.from({ length: 5 }, () => genRandomExample());
654+
```
655+
586656
## Demos
587657

588658
Run the included demos to see the library in action:

src/__demos__/templates-demo.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {TemplateJson} from '../structured/TemplateJson';
2+
import * as templates from '../examples';
3+
4+
console.log('🎲 JSON Random Template Examples\n');
5+
6+
console.log('📧 Email addresses:');
7+
for (let i = 0; i < 3; i++) {
8+
const email = TemplateJson.gen(['str', templates.tokenEmail]);
9+
console.log(` ${email}`);
10+
}
11+
12+
console.log('\n📞 Phone numbers:');
13+
for (let i = 0; i < 3; i++) {
14+
const phone = TemplateJson.gen(['str', templates.tokenPhone]);
15+
console.log(` ${phone}`);
16+
}
17+
18+
console.log('\n🏷️ Product codes:');
19+
for (let i = 0; i < 3; i++) {
20+
const code = TemplateJson.gen(['str', templates.tokenProductCode]);
21+
console.log(` ${code}`);
22+
}
23+
24+
console.log('\n👤 User profile:');
25+
const user = TemplateJson.gen(templates.userProfile);
26+
console.log(JSON.stringify(user, null, 2));
27+
28+
console.log('\n🛒 E-commerce product:');
29+
const product = TemplateJson.gen(templates.product);
30+
console.log(JSON.stringify(product, null, 2));
31+
32+
console.log('\n📋 Order:');
33+
const order = TemplateJson.gen(templates.order);
34+
console.log(JSON.stringify(order, null, 2));
35+
36+
console.log('\n🌐 API Response:');
37+
const apiResponse = TemplateJson.gen(templates.apiResponse);
38+
console.log(JSON.stringify(apiResponse, null, 2));
39+
40+
console.log('\n🏥 Patient record:');
41+
const patient = TemplateJson.gen(templates.patient);
42+
console.log(JSON.stringify(patient, null, 2));
43+
44+
console.log('\n📊 IoT Sensor reading:');
45+
const sensor = TemplateJson.gen(templates.sensorReading);
46+
console.log(JSON.stringify(sensor, null, 2));
47+
48+
console.log('\n🌳 Tree structure (recursive):');
49+
const tree = TemplateJson.gen(templates.tree());
50+
console.log(JSON.stringify(tree, null, 2));
51+
52+
console.log('\n🔀 Mixed types (or template):');
53+
for (let i = 0; i < 5; i++) {
54+
const mixed = TemplateJson.gen(templates.mixedTypes);
55+
console.log(` ${typeof mixed}: ${JSON.stringify(mixed)}`);
56+
}
57+
58+
console.log('\n🎯 Edge cases:');
59+
const empty = TemplateJson.gen(templates.emptyStructures);
60+
console.log(JSON.stringify(empty, null, 2));
61+
62+
console.log('\n📏 Large numbers:');
63+
const large = TemplateJson.gen(templates.largeNumbers);
64+
console.log(JSON.stringify(large, null, 2));
65+
66+
console.log('\n🎰 Random examples from allExamples template:');
67+
for (let i = 0; i < 3; i++) {
68+
const example = TemplateJson.gen(templates.allExamples);
69+
console.log(`Example ${i + 1}:`, JSON.stringify(example, null, 2));
70+
console.log('---');
71+
}

0 commit comments

Comments
 (0)