You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A flexible and powerful mock data generator built on top of Faker.js, designed to streamline the creation of realistic test data for your JavaScript applications.
8
+
9
+
## Features
4
10
5
-
A flexible and powerful fake data generator built on top of Faker.js.
The `factory` function always returns an array, even when generating a single item. If you need a single object, you can easily extract it using array destructuring or indexing:
This design ensures consistency and flexibility in the API, allowing you to always work with the result in the same way, regardless of the quantity generated.
62
+
63
+
## API Reference
64
+
65
+
### factory(schemaOrSample, options)
66
+
67
+
Generates mock data based on the provided schema or sample.
55
68
56
-
- Flexible schema definition
57
-
- Supports nested objects and arrays
58
-
- Custom generator functions
59
-
- Built on Faker.js for a wide range of data types
60
-
- Set a seed to generate consistent data across multiple runs
61
-
- Custom type definitions
62
-
- Schema inference
69
+
-`schemaOrSample`: Object or Array defining the structure of the data to generate, or a sample object to infer the schema from.
70
+
-`options`: Object (optional)
71
+
-`quantity`: Number of objects to generate (default: 1)
72
+
-`seed`: Seed for consistent random generation
73
+
-`isSample`: Boolean indicating if the first argument is a sample object (default: false)
74
+
-`cache`: Object for caching generator functions (advanced usage)
63
75
64
-
## Schema Definition
76
+
Returns an array of generated objects.
65
77
66
-
### Simple Array
78
+
### Defining Schemas
79
+
80
+
#### Simple Array
67
81
```javascript
68
82
constschema= ['id', 'name', 'email'];
69
83
```
70
84
71
-
### Object with Types
85
+
####Object with Types
72
86
```javascript
73
87
constschema= {
74
88
id:'uuid',
@@ -77,7 +91,7 @@ const schema = {
77
91
};
78
92
```
79
93
80
-
### Complex Object
94
+
####Complex Object
81
95
```javascript
82
96
constschema= {
83
97
id:'uniqueInt',
@@ -95,175 +109,110 @@ const schema = {
95
109
};
96
110
```
97
111
98
-
### Custom Generators
99
-
```javascript
100
-
constschema= {
101
-
id:'uuid',
102
-
name:'fullName',
103
-
customField: () =>'Custom Value'
104
-
};
105
-
```
106
-
107
-
## API
108
-
109
-
### factory(schema, quantity = 1)
110
-
111
-
-`schema`: Object or Array defining the structure of the data to generate
112
-
-`quantity`: Number of objects to generate (default: 1)
113
-
114
-
Returns an array of generated objects based on the schema.
115
-
116
-
## Supported Types
112
+
### Supported Types
117
113
118
-
- Basic: string, number, boolean
114
+
- Basic: string, number, boolean, uuid
119
115
- Person: firstName, lastName, fullName, gender, age
120
-
- Contact: email, phone
116
+
- Contact: email, phone, mobile
121
117
- Internet: username, password, url, avatar
122
118
- Address: address, street, city, state, country, zipCode, latitude, longitude
2. Using the `seed` option in the `factory` function (recommended):
146
142
147
-
// This will generate the same data every time with the same seed
148
-
console.log(users);
143
+
```javascript
144
+
import { factory } from'shujau-mock-factory';
145
+
146
+
constusers=factory(
147
+
{ name:'fullName', email:'email' },
148
+
{ quantity:2, seed:123 }
149
+
);
150
+
151
+
// This will generate the same data as the previous example
149
152
```
150
153
151
-
## Custom Type Definitions
154
+
Using the `seed` option is recommended as it allows for more localized control over seeding, especially when you need different seeds for different factory calls in the same script.
152
155
153
-
You can now define your own custom types using the `defineType` function:
The `factoryFromSample` function will infer the schema from the sample data and generate appropriate mock data. It supports nested object structures, allowing you to create complex mock data easily.
0 commit comments