Skip to content

Commit

Permalink
docs(website): extend usage section
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Sep 14, 2022
1 parent 8cd3c73 commit 00d0a64
Showing 1 changed file with 89 additions and 3 deletions.
92 changes: 89 additions & 3 deletions docs/guide/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ class User {
}
```

As you can see, your `User` model probably looks completly different from the one you have in your codebase.
As you can see, your `User` model probably looks completely different from the one you have in your codebase.
One thing to keep an eye on is the `subscriptionTier` property, as it is not simply a string, but only one of the strings defined in the `SubscriptionTier` type (`'free'` or `'basic'` or `'business'`).
Also, in a real scenario your model should not depend on a type of a third party library (`SexType` in this case).

Let's create our first user factory function.
Let's create our first user factory function:

```ts
import { faker } from '@faker-js/faker';
Expand All @@ -120,7 +120,7 @@ function createRandomUser(): User {
email: faker.internet.email(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
sex: faker.name.sexType();
sex: faker.name.sexType(),
subscriptionTier: faker.helpers.arrayElement([
'free',
'basic',
Expand All @@ -131,3 +131,89 @@ function createRandomUser(): User {

const user = createRandomUser();
```

At this point we have a perfectly working function that will work for most purposes.
But we can take this a step further.
Currently all properties are just randomly generated.
This can lead to some undesirable values being produces.
For example: The `sex` property having value `'female'` while `firstName` is `'Bob'`.

Lets refactor our current code:

```ts {4-7,13-16}
import { faker } from '@faker-js/faker';

function createRandomUser(): User {
const sex = this.faker.name.sexType();
const firstName = faker.name.firstName(sex);
const lastName = faker.name.lastName();
const email = faker.internet.email(firstName, lastName)

return {
_id: faker.datatype.uuid(),
avatar: faker.image.avatar(),
birthday: faker.date.birthdate(),
email,
firstName,
lastName,
sex,
subscriptionTier: faker.helpers.arrayElement([
'free',
'basic',
'business',
]),
};
}

const user = createRandomUser();
```

As you can see, we changed the order in which we generate our values.
First we generate a `sex` value to use it as input for the generation of `firstName`.
Then we generate the `lastName`.
Here, we could also pass in the `sex` value as argument, but in our use-case there are no special cases in where a female last name would differ from a male one.
By doing this first we are able to pass both names into the `email` generation function.
This allows the value to be more reasonable based on the provided arguments.

But we can take this even another step further.
Opposite to the `_id` property that uses an `uuid` implementation, which is unique by design, the `email` property potentially isn't.
But in most use-cases this would be desireable.

Faker got your back, with another helper method:
```ts {7-9}
import { faker } from '@faker-js/faker';

function createRandomUser(): User {
const sex = this.faker.name.sexType();
const firstName = faker.name.firstName(sex);
const lastName = faker.name.lastName();
const email = faker.helpers.unique(
faker.internet.email,
[
firstName,
lastName,
],
);

return {
_id: faker.datatype.uuid(),
avatar: faker.image.avatar(),
birthday: faker.date.birthdate(),
email,
firstName,
lastName,
sex,
subscriptionTier: faker.helpers.arrayElement([
'free',
'basic',
'business',
]),
};
}

const user = createRandomUser();
```

By wrapping Faker's `email` function with the [`unique`](../api/helpers.md#unique) helper function we ensure that the return value of `email` is always unique.

Congratulation, you should now be able to create any complex object you desire. Happy faking 🥳.

0 comments on commit 00d0a64

Please sign in to comment.