Skip to content

Commit 6a4fba8

Browse files
Copilotstreamich
andcommitted
fix: address code review feedback - remove examples export, fix function calls, add helper methods
Co-authored-by: streamich <[email protected]>
1 parent d54454e commit 6a4fba8

File tree

3 files changed

+210
-3
lines changed

3 files changed

+210
-3
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/examples.ts

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {Token} from './string';
22
import type {StringTemplate, Template, ObjectTemplate, ArrayTemplate, MapTemplate} from './structured/types';
3+
import {TemplateJson} from './structured/TemplateJson';
34

45
// ============================================================================
56
// String Pattern Templates (from README examples)
@@ -1088,6 +1089,143 @@ export const allExamples: Template = [
10881089
performanceTest,
10891090
mixedTypes,
10901091
loadTestUser,
1091-
tree(),
1092-
comment(),
1092+
tree,
1093+
comment,
10931094
];
1095+
1096+
// ============================================================================
1097+
// Helper Methods for Easy Random JSON Generation
1098+
// ============================================================================
1099+
1100+
/**
1101+
* Generate a random user profile with comprehensive details.
1102+
* @returns Random user profile object
1103+
*/
1104+
export const genUser = () => TemplateJson.gen(userProfile);
1105+
1106+
/**
1107+
* Generate a basic user object with essential information.
1108+
* @returns Random basic user object
1109+
*/
1110+
export const genUserBasic = () => TemplateJson.gen(userBasic);
1111+
1112+
/**
1113+
* Generate a random address object with street, city, state, etc.
1114+
* @returns Random address object
1115+
*/
1116+
export const genAddress = () => TemplateJson.gen(address);
1117+
1118+
/**
1119+
* Generate a random product with details like name, price, category.
1120+
* @returns Random product object
1121+
*/
1122+
export const genProduct = () => TemplateJson.gen(product);
1123+
1124+
/**
1125+
* Generate a random order with items, customer info, and totals.
1126+
* @returns Random order object
1127+
*/
1128+
export const genOrder = () => TemplateJson.gen(order);
1129+
1130+
/**
1131+
* Generate a random financial transaction.
1132+
* @returns Random transaction object
1133+
*/
1134+
export const genTransaction = () => TemplateJson.gen(transaction);
1135+
1136+
/**
1137+
* Generate a random bank account information.
1138+
* @returns Random bank account object
1139+
*/
1140+
export const genBankAccount = () => TemplateJson.gen(bankAccount);
1141+
1142+
/**
1143+
* Generate a random social media post.
1144+
* @returns Random social post object
1145+
*/
1146+
export const genSocialPost = () => TemplateJson.gen(socialPost);
1147+
1148+
/**
1149+
* Generate a random social media profile.
1150+
* @returns Random social profile object
1151+
*/
1152+
export const genSocialProfile = () => TemplateJson.gen(socialProfile);
1153+
1154+
/**
1155+
* Generate a random location with coordinates and details.
1156+
* @returns Random location object
1157+
*/
1158+
export const genLocation = () => TemplateJson.gen(location);
1159+
1160+
/**
1161+
* Generate a random API response with data array.
1162+
* @returns Random API response object
1163+
*/
1164+
export const genApiResponse = () => TemplateJson.gen(apiResponse);
1165+
1166+
/**
1167+
* Generate a detailed API response with comprehensive metadata.
1168+
* @returns Random detailed API response object
1169+
*/
1170+
export const genApiResponseDetailed = () => TemplateJson.gen(apiResponseDetailed);
1171+
1172+
/**
1173+
* Generate a random service configuration.
1174+
* @returns Random service config object
1175+
*/
1176+
export const genServiceConfig = () => TemplateJson.gen(serviceConfig);
1177+
1178+
/**
1179+
* Generate a random medical patient record.
1180+
* @returns Random patient object
1181+
*/
1182+
export const genPatient = () => TemplateJson.gen(patient);
1183+
1184+
/**
1185+
* Generate a comprehensive medical record.
1186+
* @returns Random medical record object
1187+
*/
1188+
export const genMedicalRecord = () => TemplateJson.gen(medicalRecord);
1189+
1190+
/**
1191+
* Generate a random student profile.
1192+
* @returns Random student object
1193+
*/
1194+
export const genStudent = () => TemplateJson.gen(student);
1195+
1196+
/**
1197+
* Generate a random course information.
1198+
* @returns Random course object
1199+
*/
1200+
export const genCourse = () => TemplateJson.gen(course);
1201+
1202+
/**
1203+
* Generate a random IoT sensor reading.
1204+
* @returns Random sensor reading object
1205+
*/
1206+
export const genSensorReading = () => TemplateJson.gen(sensorReading);
1207+
1208+
/**
1209+
* Generate a random IoT device profile.
1210+
* @returns Random IoT device object
1211+
*/
1212+
export const genIotDevice = () => TemplateJson.gen(iotDevice);
1213+
1214+
/**
1215+
* Generate a random log entry for monitoring.
1216+
* @returns Random log entry object
1217+
*/
1218+
export const genLogEntry = () => TemplateJson.gen(logEntry);
1219+
1220+
/**
1221+
* Generate random metric data for monitoring.
1222+
* @returns Random metric data object
1223+
*/
1224+
export const genMetricData = () => TemplateJson.gen(metricData);
1225+
1226+
/**
1227+
* Generate a random example from any of the available templates.
1228+
* Uses the 'or' pattern to randomly select from all templates.
1229+
* @returns Random example data from any template
1230+
*/
1231+
export const genRandomExample = () => TemplateJson.gen(allExamples);

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ export * from './RandomJson';
33
export * from './number';
44
export * from './string';
55
export * from './structured';
6-
export * as examples from './examples';

0 commit comments

Comments
 (0)