Skip to content

feat: Organize comprehensive templates in examples.ts with helper methods and combined template #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,76 @@ const serviceConfig = TemplateJson.gen(['obj', [
]]);
```

## Helper Methods for Easy Generation

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:

```typescript
import {
genUser,
genAddress,
genProduct,
genOrder,
genRandomExample
} from '@jsonjoy.com/json-random/lib/examples';

// Generate common data types quickly
const user = genUser();
const address = genAddress();
const product = genProduct();
const order = genOrder();

// Generate random example from any template
const randomData = genRandomExample();
```

### Available Helper Methods

| Method | Description |
|--------|-------------|
| `genUser()` | Generate comprehensive user profile with details |
| `genUserBasic()` | Generate basic user with essential information |
| `genAddress()` | Generate address with street, city, state, etc. |
| `genProduct()` | Generate product with name, price, category |
| `genOrder()` | Generate order with items and customer info |
| `genTransaction()` | Generate financial transaction data |
| `genBankAccount()` | Generate bank account information |
| `genSocialPost()` | Generate social media post |
| `genSocialProfile()` | Generate social media profile |
| `genLocation()` | Generate location with coordinates |
| `genApiResponse()` | Generate API response with data array |
| `genApiResponseDetailed()` | Generate comprehensive API response |
| `genServiceConfig()` | Generate service configuration |
| `genPatient()` | Generate medical patient record |
| `genMedicalRecord()` | Generate comprehensive medical record |
| `genStudent()` | Generate student profile |
| `genCourse()` | Generate course information |
| `genSensorReading()` | Generate IoT sensor reading |
| `genIotDevice()` | Generate IoT device profile |
| `genLogEntry()` | Generate log entry for monitoring |
| `genMetricData()` | Generate metric data for monitoring |
| `genRandomExample()` | Generate random data from any available template |

### Usage Examples

```typescript
// Generate test user data for API testing
const testUser = genUser();
console.log(testUser);
// Output: { id: 4829, username: "user_7432", email: "[email protected]", ... }

// Generate address for form testing
const shippingAddress = genAddress();
console.log(shippingAddress);
// Output: { street: "123 Main St", city: "Springfield", state: "CA", ... }

// Generate product catalog
const products = Array.from({ length: 10 }, () => genProduct());

// Generate random test data
const randomTestData = Array.from({ length: 5 }, () => genRandomExample());
```

## Demos

Run the included demos to see the library in action:
Expand Down
71 changes: 71 additions & 0 deletions src/__demos__/templates-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {TemplateJson} from '../structured/TemplateJson';
import * as templates from '../examples';

console.log('🎲 JSON Random Template Examples\n');

console.log('📧 Email addresses:');
for (let i = 0; i < 3; i++) {
const email = TemplateJson.gen(['str', templates.tokenEmail]);
console.log(` ${email}`);
}

console.log('\n📞 Phone numbers:');
for (let i = 0; i < 3; i++) {
const phone = TemplateJson.gen(['str', templates.tokenPhone]);
console.log(` ${phone}`);
}

console.log('\n🏷️ Product codes:');
for (let i = 0; i < 3; i++) {
const code = TemplateJson.gen(['str', templates.tokenProductCode]);
console.log(` ${code}`);
}

console.log('\n👤 User profile:');
const user = TemplateJson.gen(templates.userProfile);
console.log(JSON.stringify(user, null, 2));

console.log('\n🛒 E-commerce product:');
const product = TemplateJson.gen(templates.product);
console.log(JSON.stringify(product, null, 2));

console.log('\n📋 Order:');
const order = TemplateJson.gen(templates.order);
console.log(JSON.stringify(order, null, 2));

console.log('\n🌐 API Response:');
const apiResponse = TemplateJson.gen(templates.apiResponse);
console.log(JSON.stringify(apiResponse, null, 2));

console.log('\n🏥 Patient record:');
const patient = TemplateJson.gen(templates.patient);
console.log(JSON.stringify(patient, null, 2));

console.log('\n📊 IoT Sensor reading:');
const sensor = TemplateJson.gen(templates.sensorReading);
console.log(JSON.stringify(sensor, null, 2));

console.log('\n🌳 Tree structure (recursive):');
const tree = TemplateJson.gen(templates.tree());
console.log(JSON.stringify(tree, null, 2));

console.log('\n🔀 Mixed types (or template):');
for (let i = 0; i < 5; i++) {
const mixed = TemplateJson.gen(templates.mixedTypes);
console.log(` ${typeof mixed}: ${JSON.stringify(mixed)}`);
}

console.log('\n🎯 Edge cases:');
const empty = TemplateJson.gen(templates.emptyStructures);
console.log(JSON.stringify(empty, null, 2));

console.log('\n📏 Large numbers:');
const large = TemplateJson.gen(templates.largeNumbers);
console.log(JSON.stringify(large, null, 2));

console.log('\n🎰 Random examples from allExamples template:');
for (let i = 0; i < 3; i++) {
const example = TemplateJson.gen(templates.allExamples);
console.log(`Example ${i + 1}:`, JSON.stringify(example, null, 2));
console.log('---');
}
Loading