Skip to content

Commit

Permalink
remove the unique section from usage.md and reference new page
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmayer committed Feb 14, 2024
1 parent c842c3b commit 4d6bbf6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 55 deletions.
22 changes: 7 additions & 15 deletions docs/guide/unique.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ Basic example:
const name = faker.helpers.unique(faker.person.firstName);

//NEW
import {
UniqueEnforcer
} from 'enforce-unique';
import { UniqueEnforcer } from 'enforce-unique';
//const { UniqueEnforcer } = require("enforce-unique") //CJS
const enforcerName = new UniqueEnforcer();
const name = enforcerName.enforce(faker.person.firstName);
Expand All @@ -88,9 +86,7 @@ const stateCode = faker.helpers.unique(faker.location.state, [
]);

//NEW
import {
UniqueEnforcer
} from 'enforce-unique';
import { UniqueEnforcer } from 'enforce-unique';

const enforcerState = new UniqueEnforcer();
const stateCode = enforcerState.enforce(() => {
Expand All @@ -110,16 +106,12 @@ const city = faker.helpers.unique(faker.location.city, [], {
});

//NEW
import {
UniqueEnforcer
} from 'enforce-unique';
import { UniqueEnforcer } from 'enforce-unique';
const enforcer = new UniqueEnforcer();
const city = enforcer.enforce(
faker.location.city, {
maxRetries: 100,
maxTime: 1000,
}
);
const city = enforcer.enforce(faker.location.city, {
maxRetries: 100,
maxTime: 1000,
});
```

Note `enforce-unique` does not support the `exclude` or `store` options. If you were previously using these, you may wish to build your own uniue logic instead.
41 changes: 1 addition & 40 deletions docs/guide/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,45 +233,6 @@ Here, we could also pass in the `sex` value as argument, but in our use-case the
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 desirable.

Faker has your back, with another helper method:

```ts {7-10}
import { faker } from '@faker-js/faker';

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

return {
_id: faker.string.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.

::: warning
The `faker.helpers.unique` is targeted to be removed from Faker in the future.
Please have a look at the issue [#1785](https://github.com/faker-js/faker/issues/1785).
We will update these docs once a replacement is available.
:::
Unlike the `_id` property that uses an `uuid` implementation, which is unique by design, the `email` may contain duplicates. 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 🥳.

0 comments on commit 4d6bbf6

Please sign in to comment.