-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: introduce FakerCore #2838
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 all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
24a25ba
feat: introduce FakerCore
ST-DDT 1688003
chore: restore
ST-DDT 2ef0270
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 790e645
chore: apply suggestions
ST-DDT e4da2f5
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 6ef3999
chore: todos
ST-DDT 66f4696
chore: workaround
ST-DDT 7353a69
Merge branch 'next' into feat/samfn/faker-core
ST-DDT f96a407
Merge branch 'next' into feat/samfn/faker-core
ST-DDT d99d3b4
Merge branch 'next' into feat/samfn/faker-core
ST-DDT aa8d8d6
Merge branch 'next' into feat/samfn/faker-core
ST-DDT a54120a
Merge branch 'next' into feat/samfn/faker-core
ST-DDT e807a8e
Merge branch 'next' into feat/samfn/faker-core
ST-DDT e659674
Merge branch 'next' into feat/samfn/faker-core
ST-DDT df2f308
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 6dada60
chore: cleanup imports
ST-DDT 4f9907a
chore: simplify FakerOptions
ST-DDT 24bb1bf
chore: export new functions and types
ST-DDT b2bf1e2
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 54add10
chore: edit after review
ST-DDT 49ee0fb
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 9a12eee
Merge branch 'next' into feat/samfn/faker-core
ST-DDT 1f3f42c
chore: remove comment
ST-DDT 0c2e3de
chore: bump since version
ST-DDT 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * The possible configuration options, that can be set. | ||
| * This type exists to be extended for plugins via type augmentation. | ||
| * | ||
| * The `@default` tag is used to indicate the default value, that should be used if, the config is absent. | ||
| */ | ||
| export interface FakerConfig { | ||
| /** | ||
| * The function used to generate the `refDate` date instance, if not provided as method param. | ||
| * The function must return a new valid `Date` instance for every call. | ||
| * | ||
| * @see [Reproducible Results](https://fakerjs.dev/guide/usage.html#reproducible-results) | ||
| * @see faker.seed(): For generating reproducible values. | ||
| * | ||
| * @since 9.0.0 | ||
| * | ||
| * @default () => new Date() | ||
| */ | ||
| defaultRefDate?: () => Date; | ||
| } |
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,102 @@ | ||
| import type { FakerConfig } from './config'; | ||
| import type { LocaleDefinition } from './definitions'; | ||
| import type { Randomizer } from './randomizer'; | ||
| import { mergeLocales } from './utils/merge-locales'; | ||
| import { generateMersenne53Randomizer } from './utils/mersenne'; | ||
|
|
||
| /** | ||
| * The core grants access to the locale data, the randomizer and config settings. | ||
| */ | ||
| export interface FakerCore { | ||
| /** | ||
| * The locale data associated with this instance. | ||
| * | ||
| * Always present, but it might be empty if the locale data is not available. | ||
| */ | ||
| readonly locale: LocaleDefinition; | ||
|
|
||
| /** | ||
| * The randomizer used to generate random values. | ||
| */ | ||
| readonly randomizer: Randomizer; | ||
|
|
||
| /** | ||
| * The configuration settings used by this instance. | ||
| */ | ||
| readonly config: FakerConfig; | ||
| } | ||
|
|
||
| export interface FakerOptions { | ||
| /** | ||
| * The locale definitions to use. If not provided, this core will not have any locale data and thus all methods that rely on locale data will throw an error when called. | ||
| * | ||
| * @default {} | ||
| */ | ||
| locale?: LocaleDefinition | LocaleDefinition[]; | ||
| /** | ||
| * The randomizer used to generate random values. | ||
| * | ||
| * @default generateMersenne53Randomizer() | ||
| */ | ||
| randomizer?: Randomizer; | ||
| /** | ||
| * The configuration options for all methods. | ||
| * | ||
| * @default {} | ||
| */ | ||
| config?: FakerConfig; | ||
| /** | ||
| * The initial seed to use. | ||
| * The seed can be used to generate reproducible values. | ||
| * | ||
| * Refer to the `seed()` method for more information. | ||
| * | ||
| * Defaults to a random seed. | ||
| */ | ||
| seed?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to create a FakerCore instance. | ||
| * | ||
| * @param options The options to create the FakerCore instance with. | ||
| * @param options.locale The locale definitions to use. | ||
| * If not provided, this core will not have any locale data and thus all methods that rely on locale data will throw an error when called. | ||
| * This can be useful if you want to use least amount of memory possible and only use methods that do not rely on locale data. | ||
| * @param options.randomizer The randomizer used to generate random values. | ||
| * Defaults to `generateMersenne53Randomizer()`. | ||
| * @param options.config The configuration options for all methods. | ||
| * Defaults to an empty config. | ||
| * @param options.seed The initial seed to use. | ||
| * The seed can be used to generate reproducible values. | ||
| * Refer to the `seed()` method for more information. | ||
| * Defaults to a random seed. | ||
| * | ||
| * @returns The newly created FakerCore instance. | ||
| * | ||
| * @example | ||
| * import { createFakerCore, en } from '@faker-js/faker'; | ||
| * | ||
| * createFakerCore() // no locale data, default randomizer and empty config | ||
| * createFakerCore({ locale: en }) // custom locale data, default randomizer and empty config | ||
|
ST-DDT marked this conversation as resolved.
|
||
| * | ||
| * @since 10.5.0 | ||
| */ | ||
| export function createFakerCore(options: FakerOptions = {}): FakerCore { | ||
| const { | ||
| locale = {}, | ||
| randomizer = generateMersenne53Randomizer(), | ||
| config = {}, | ||
| seed, | ||
| } = options; | ||
|
|
||
| if (randomizer != null && seed != null) { | ||
| randomizer.seed(seed); | ||
| } | ||
|
|
||
| return { | ||
| locale: Array.isArray(locale) ? mergeLocales(locale) : locale, | ||
| randomizer, | ||
| config, | ||
| }; | ||
| } | ||
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
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
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.
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.