Skip to content

Commit

Permalink
Fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Feb 14, 2024
1 parent d650a59 commit ff38a0c
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions docs/guide/unique.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ faker.animal.type(); //'horse'
faker.animal.type(); //'horse'
```

Some methods and locales use a much larger data sets than others. For example `faker.animal.type` has only 13 possible animals to choose from. By contrast `faker.person.fullName()` pulls from a list of hundreds of first names, surnames and prefixes/suffixes, so can generate hundreds of thousands of unique names. Even then, the [birthday paradox](https://en.wikipedia.org/wiki/Birthday_Paradox) means that duplicate values will quickly be generated.
Some methods and locales use a much larger data sets than others. For example `faker.animal.type` has only 13 possible animals to choose from. By contrast `faker.person.fullName()` pulls from a list of hundreds of first names, surnames and prefixes/suffixes, so it can generate hundreds of thousands of unique names. Even then, the [birthday paradox](https://en.wikipedia.org/wiki/Birthday_Paradox) means that duplicate values will quickly be generated.

Sometimes you want to generate unique values, for example you may wish to have unique values in an database email column. There are a few possible strategies for this:
Sometimes you want to generate unique values, for example you may wish to have unique values in a database email column.
There are a few possible strategies for this:

1. Use `faker.helpers.uniqueArray()` if you want to generate all the values at one time. For example

Expand All @@ -28,20 +29,20 @@ faker.helpers.uniqueArray(faker.internet.email, 1000); // will generate 1000 uni

Note you can supply a maximum time (in milliseconds) or maximum number of retries.

```ts
```js
import { EnforceUniqueError, UniqueEnforcer } from 'enforce-unique';

const uniqueEnforcerEmail = new UniqueEnforcer();

function createRandomUser() {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const email = uniqueEnforcerEmail.enforce(
() => {
return faker.internet.email({
() =>
faker.internet.email({
firstName,
lastName,
});
},
}),
{
maxTime: 50,
maxRetries: 50,
Expand All @@ -64,20 +65,21 @@ In general you should switch to using one of the strategies above. For example,

Basic example:

```ts
```js
//OLD
const name = faker.helpers.unique(faker.person.firstName);

//NEW
import { UniqueEnforcer } from 'enforce-unique';
//const { UniqueEnforcer } = require("enforce-unique") //CJS

const enforcerName = new UniqueEnforcer();
const name = enforcerName.enforce(faker.person.firstName);
```

With parameters:

```ts
```js
//OLD
const stateCode = faker.helpers.unique(faker.location.state, [
{
Expand All @@ -89,16 +91,16 @@ const stateCode = faker.helpers.unique(faker.location.state, [
import { UniqueEnforcer } from 'enforce-unique';

const enforcerState = new UniqueEnforcer();
const stateCode = enforcerState.enforce(() => {
return faker.location.state({
const stateCode = enforcerState.enforce(() =>
faker.location.state({
abbreviated: true,
});
});
})
);
```

With options:

```ts
```js
//OLD
const city = faker.helpers.unique(faker.location.city, [], {
maxRetries: 100,
Expand All @@ -107,11 +109,12 @@ const city = faker.helpers.unique(faker.location.city, [], {

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

const enforcer = new UniqueEnforcer();
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.
Note `enforce-unique` does not support the `exclude` or `store` options. If you were previously using these, you may wish to build your own unique logic instead.

0 comments on commit ff38a0c

Please sign in to comment.