struct-fakerator 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -170,5 +170,44 @@ flowchart TB
|
|
|
170
170
|
value --> string
|
|
171
171
|
value --> email
|
|
172
172
|
value --> other[...]
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
但並不是所有人情況都能自己手動建立函數,有可能是開放給別人使用的服務,沒辦法在使用方建立函數,這時 `createGeneratorByType` 第二個可以讓製作服務的人帶入自己的擴充,這樣這個函數就能接受更多種型態。
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
const createIntValueConfig = (option) => createValueConfig(() => faker.number.int(option));
|
|
179
|
+
const createEmailValueConfig = (option) => createValueConfig(() => faker.internet.email(option));
|
|
180
|
+
|
|
181
|
+
const customTypeMatch = (config) => {
|
|
182
|
+
if (config.type === "int") {
|
|
183
|
+
return createIntValueConfig(config.option);
|
|
184
|
+
}
|
|
185
|
+
if (config.type === "email") {
|
|
186
|
+
return createEmailValueConfig(config.option);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
throw Error("error");
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const config = {
|
|
193
|
+
type: "obj",
|
|
194
|
+
content: {
|
|
195
|
+
name: { type: "value", generateFn: () => "John" },
|
|
196
|
+
age: { type: "int" },
|
|
197
|
+
email: { type: "email" },
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const result = createGeneratorByType(config, customTypeMatch)();
|
|
202
|
+
|
|
203
|
+
console.log(result);
|
|
204
|
+
|
|
205
|
+
/*
|
|
206
|
+
{
|
|
207
|
+
name: "John",
|
|
208
|
+
age: 50,
|
|
209
|
+
email: "xxx@example.com",
|
|
210
|
+
}
|
|
211
|
+
*/
|
|
212
|
+
```
|
|
173
213
|
|
|
174
|
-
```
|