-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path01-basic-usage.ts
More file actions
86 lines (71 loc) · 2.27 KB
/
01-basic-usage.ts
File metadata and controls
86 lines (71 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Basic Usage Example
*
* This example demonstrates the fundamental features of Interface-Forge:
* - Creating a simple factory
* - Building single instances
* - Building batches
* - Overriding default values
*/
import { Factory } from 'interface-forge';
interface User {
age: number;
createdAt: Date;
email: string;
id: string;
isActive: boolean;
name: string;
}
const UserFactory = new Factory<User>((factory) => ({
age: factory.number.int({ max: 80, min: 18 }),
createdAt: factory.date.past(),
email: factory.internet.email(),
id: factory.string.uuid(),
isActive: factory.datatype.boolean(),
name: factory.person.fullName(),
}));
const user = UserFactory.build();
console.log('Single user:', user);
const users = UserFactory.batch(5);
console.log(`Generated ${users.length} users`);
const customUser = UserFactory.build({
email: 'john@example.com',
isActive: true,
name: 'John Doe',
});
console.log('Custom user:', customUser);
const activeUsers = UserFactory.batch(3, {
age: 25,
isActive: true,
});
console.log('Active users:', activeUsers);
// Context-aware factory example
interface Person {
age: number;
canDrive: boolean;
canVote: boolean;
name: string;
}
const PersonFactory = new Factory<Person>((factory, _iteration, kwargs) => {
// Use provided age or generate one
const age = kwargs?.age ?? factory.number.int({ max: 80, min: 0 });
return {
age,
canDrive: age >= 16, // Context-aware: depends on actual age
canVote: age >= 18, // Context-aware: depends on actual age
name: kwargs?.name ?? factory.person.fullName(),
};
});
console.log('\n=== Context-Aware Examples ===');
const child = PersonFactory.build({ age: 10 });
console.log('Child:', child);
// { age: 10, canDrive: false, canVote: false, name: "..." }
const teenager = PersonFactory.build({ age: 17 });
console.log('Teenager:', teenager);
// { age: 17, canDrive: true, canVote: false, name: "..." }
const adult = PersonFactory.build({ age: 25 });
console.log('Adult:', adult);
// { age: 25, canDrive: true, canVote: true, name: "..." }
const companyName = UserFactory.company.name();
const futureDate = UserFactory.date.future();
console.log('\nCompany:', companyName, 'Future date:', futureDate);