aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-10-21 16:22:58 +0200
committerGitHub <[email protected]>2024-10-21 14:22:58 +0000
commit0d1616dd1e07538cc2d2f62c34ebfcf1298dd3a3 (patch)
tree1acea458dbe089688912ac245defead1113ab736
parentf917f4fd348d2cce7883dc2b0d46c4dda72ec442 (diff)
downloadfaker-0d1616dd1e07538cc2d2f62c34ebfcf1298dd3a3.tar.xz
faker-0d1616dd1e07538cc2d2f62c34ebfcf1298dd3a3.zip
docs(guide): add complex overwrites example (#3207)
-rw-r--r--docs/guide/usage.md47
1 files changed, 46 insertions, 1 deletions
diff --git a/docs/guide/usage.md b/docs/guide/usage.md
index 9b5bbf93..6d33655c 100644
--- a/docs/guide/usage.md
+++ b/docs/guide/usage.md
@@ -260,4 +260,49 @@ This allows the value to be more reasonable based on the provided arguments.
Unlike the `_id` property that uses an `uuid` implementation, which has a low chance of duplicates, the `email` function is more likely to produce duplicates, especially if the call arguments are similar. We have a dedicated guide page on generating [unique values](unique).
-Congratulations, you should now be able to create any complex object you desire. Happy faking 🥳.
+The example above demonstrates how to generate complex objects.
+To gain more control over the values of specific properties, you can introduce `overwrites`, `options` or similar parameters:
+
+```ts {3,17}
+import { faker } from '@faker-js/faker';
+
+function createRandomUser(overwrites: Partial<User> = {}): User {
+ const {
+ _id = faker.string.uuid(),
+ avatar = faker.image.avatar(),
+ birthday = faker.date.birthdate(),
+ sex = faker.person.sexType(),
+ firstName = faker.person.firstName(sex),
+ lastName = faker.person.lastName(),
+ email = faker.internet.email({ firstName, lastName }),
+ subscriptionTier = faker.helpers.arrayElement([
+ 'free',
+ 'basic',
+ 'business',
+ ]),
+ } = overwrites;
+
+ return {
+ _id,
+ avatar,
+ birthday,
+ email,
+ firstName,
+ lastName,
+ sex,
+ subscriptionTier,
+ };
+}
+
+const user = createRandomUser();
+const userToReject = createRandomUser({ birthday: new Date('2124-10-20') });
+```
+
+A potential `options` parameter could be used to:
+
+- control which optional properties are included,
+- control how nested elements and arrays are merged or replaced,
+- or specify the number of items to generate for nested lists.
+
+Congratulations, you should now be able to create any complex object you desire.
+Happy faking 🥳.