-
-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(helpers)!: remove v8 deprecated unique (#2661)
Co-authored-by: Eric Cheng <[email protected]> Co-authored-by: Matt Mayer <[email protected]>
- Loading branch information
1 parent
fd05126
commit 4382fd9
Showing
8 changed files
with
128 additions
and
495 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Unique Values | ||
|
||
In general, Faker methods do not return unique values. | ||
|
||
```ts | ||
faker.seed(55); | ||
faker.animal.type(); //'cat' | ||
faker.animal.type(); //'bird' | ||
faker.animal.type(); //'horse' | ||
faker.animal.type(); //'horse' | ||
``` | ||
|
||
Some methods and locales use much smaller data sets than others. For example, `faker.animal.type` has only 13 possible animals to choose from. In 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 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: | ||
|
||
```ts | ||
faker.helpers.uniqueArray(faker.internet.email, 1000); // will generate 1000 unique email addresses | ||
``` | ||
|
||
2. If there are insufficient values for your needs, consider prefixing or suffixing values with your own sequential values, for example you could prefix `1.`, `2.` to each generated email in turn. | ||
|
||
3. Build your own logic to keep track of a set of previously generated values and regenerate values as necessary if a duplicate is generated | ||
|
||
4. Use a third party package to enforce uniqueness such as [enforce-unique](https://github.com/MansurAliKoroglu/enforce-unique) | ||
|
||
Note you can supply a maximum time (in milliseconds) or maximum number of retries. | ||
|
||
```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( | ||
() => | ||
faker.internet.email({ | ||
firstName, | ||
lastName, | ||
}), | ||
{ | ||
maxTime: 50, | ||
maxRetries: 50, | ||
} | ||
); | ||
|
||
return { | ||
firstName, | ||
lastName, | ||
email, | ||
}; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
### Remove helpers.unique | ||
|
||
Prior to v9, Faker provided a [`faker.helpers.unique()`](https://v8.fakerjs.dev/api/helpers.html#unique) method which had a global store to keep track of duplicates. This was removed in v9. | ||
|
||
Please see the [unique values guide](/guide/unique) for alternatives. | ||
|
||
For example, many simple use cases can use [`faker.helpers.uniqueArray`](https://v8.fakerjs.dev/api/helpers.html#uniqueArray). Or you can migrate to a third party package such as `enforce-unique`: | ||
|
||
Basic example: | ||
|
||
```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: | ||
|
||
```js | ||
// OLD | ||
const stateCode = faker.helpers.unique(faker.location.state, [ | ||
{ | ||
abbreviated: true, | ||
}, | ||
]); | ||
|
||
// NEW | ||
import { UniqueEnforcer } from 'enforce-unique'; | ||
|
||
const enforcerState = new UniqueEnforcer(); | ||
const stateCode = enforcerState.enforce(() => | ||
faker.location.state({ | ||
abbreviated: true, | ||
}) | ||
); | ||
``` | ||
|
||
With options: | ||
|
||
```js | ||
// OLD | ||
const city = faker.helpers.unique(faker.location.city, [], { | ||
maxRetries: 100, | ||
maxTime: 1000, | ||
}); | ||
|
||
// NEW | ||
import { UniqueEnforcer } from 'enforce-unique'; | ||
|
||
const enforcer = new UniqueEnforcer(); | ||
const city = enforcer.enforce(faker.location.city, { | ||
maxRetries: 100, | ||
maxTime: 1000, | ||
}); | ||
``` | ||
|
||
::: tip 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. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.