-
-
Notifications
You must be signed in to change notification settings - Fork 977
refactor(helpers)!: remove v8 deprecated unique #2661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
837471c
refactor(helpers)!: remove v8 deprecated unique
Shinigami92 d650a59
Add documentation on recommended ways to generate unique values
matthewmayer ff38a0c
Fix typos
Shinigami92 5b1735d
Add migration file
Shinigami92 962063d
Merge branch 'next' into remove-deprecated-unique
Shinigami92 93d53d9
Update docs/guide/usage.md
Shinigami92 0b2b9e1
Merge branch 'next' into remove-deprecated-unique
Shinigami92 313010c
move migration info to migration guide
matthewmayer 13d6cf4
Merge branch 'next' into remove-deprecated-unique
Shinigami92 69fb2da
Merge branch 'next' into remove-deprecated-unique
Shinigami92 a84ad22
Update unique.md
matthewmayer 0137ff2
Merge branch 'next' into remove-deprecated-unique
Shinigami92 4ad3896
Apply review suggestions
Shinigami92 3088b39
Merge branch 'next' into remove-deprecated-unique
Shinigami92 2fc6591
Apply suggestions from code review
Shinigami92 ef40320
Merge branch 'next' into remove-deprecated-unique
Shinigami92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,120 @@ | ||
# 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 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 a database email column. | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```ts | ||
faker.helpers.uniqueArray(faker.internet.email, 1000); // will generate 1000 unique email addresses | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
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, | ||
}; | ||
} | ||
``` | ||
|
||
## Migrating from faker.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. | ||
|
||
In general you should switch to using one of the strategies above. For example, migrating to `enforce-unique`: | ||
matthewmayer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Basic example: | ||
|
||
```js | ||
//OLD | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
}); | ||
``` | ||
|
||
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. | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,5 @@ | ||
### Remove helpers.unique | ||
|
||
The `helpers.unique` method has been removed. | ||
|
||
Please see the [unique guide](/guide/unique) for more and alternatives. |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.