-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add first standalone module functions #3818
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 10 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
30e56f9
feat: introduce fakerRegistry
ST-DDT e6a75d2
docs: prepare for SMFs
ST-DDT 0656236
refactor: transform basics
ST-DDT 65e54b5
test: enable SMF refresh test
ST-DDT d1b7121
Merge branch 'next' into feat/samfn/utils
ST-DDT 4a50173
chore: replace SMF term
ST-DDT c06e403
chore: explicit registry typing
ST-DDT df682b8
docs: fix apidocs refresh
ST-DDT 48f88f6
Update src/registry.types.ts
ST-DDT b844881
chore: edit after review
ST-DDT 5100ade
Merge branch 'next' into feat/samfn/utils
ST-DDT 36d13af
chore: hide default value
ST-DDT dd4090e
chore: use code groups for examples
ST-DDT f25f8d7
chore: simplify code
ST-DDT a739b8f
chore: fix examples
ST-DDT 8bc25a8
Update test/scripts/apidocs/verify-jsdoc-tags.spec.ts
ST-DDT aae7f8b
Update test/scripts/apidocs/verify-jsdoc-tags.spec.ts
ST-DDT 87eab2a
Merge branch 'next' into feat/samfn/utils
ST-DDT f9b1812
Merge branch 'next' into feat/samfn/utils
ST-DDT 4dfd622
Merge branch 'next' into feat/samfn/utils
ST-DDT 14f8028
Merge branch 'next' into feat/samfn/utils
ST-DDT b8a1cd0
chore: rename for consistency
ST-DDT b328002
Merge branch 'next' into feat/samfn/utils
ST-DDT 60eb425
refactor: remove registry
ST-DDT 1079527
chore: cleanup
ST-DDT 563e71f
chore: update snapshots
ST-DDT 11ffcf4
chore: extend tests
ST-DDT e25a39d
chore: cleanup
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
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
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
|
xDivisionByZerox marked this conversation as resolved.
Outdated
|
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,16 @@ | ||
| import type { FakerRegistry } from './registry.types'; | ||
| import { utilsModule as utils } from './utils/registry'; | ||
|
|
||
| // TODO @ST-DDT 2026-04-12: This file will be auto generated in a future PR. | ||
|
|
||
| /** | ||
| * Global registry for the Faker library, containing all module registries with their standalone module functions. | ||
| * | ||
| * You normally don't need to access this registry unless you want to call `fake()` or other custom code needing to lookup standalone functions. | ||
| * | ||
| * @example | ||
| * fake(fakerCore, 'The date is {{utils.getDefaultRefDate}}', [ fakerRegistry, fakerCore.locale.raw ]); | ||
| */ | ||
| export const fakerRegistry = { | ||
| utils, | ||
| } as const satisfies FakerRegistry; |
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,11 @@ | ||
| import type { FakerCore } from './core'; | ||
|
|
||
| /** | ||
| * Global Registry for the Faker library, containing all module registries. | ||
| */ | ||
| export type FakerRegistry = Record<string, ModuleRegistry>; | ||
|
|
||
| /** | ||
| * Per module registry containing the module's standalone functions. | ||
| */ | ||
| export type ModuleRegistry = Record<string, (fakerCore: FakerCore) => unknown>; | ||
|
ST-DDT marked this conversation as resolved.
Outdated
|
||
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,39 @@ | ||
| import type { FakerCore } from '../core'; | ||
|
|
||
| export const DEFAULT_REF_DATE_SOURCE: () => Date = () => new Date(); | ||
|
ST-DDT marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Gets a new reference date used to generate relative dates. | ||
| * | ||
| * If `fakerCore.config.defaultRefDate` is defined, it will be used to get the default reference date. Otherwise, the current date will be used. | ||
| * | ||
| * @param fakerCore The FakerCore instance to get it from. | ||
| * | ||
| * @returns The newly created default reference date. | ||
| * | ||
| * @example | ||
| * fakerCore.randomizer.seed(1234); | ||
| * | ||
| * // Default behavior | ||
| * // setDefaultRefDate(fakerCore); | ||
| * past(fakerCore); // Changes based on the current date/time | ||
| * | ||
| * // Use a static ref date | ||
| * setDefaultRefDate(fakerCore, new Date('2020-01-01')); | ||
| * past(fakerCore); // Reproducible '2019-07-03T08:27:58.118Z' | ||
|
ST-DDT marked this conversation as resolved.
Outdated
|
||
| * | ||
| * // Use a ref date that changes every time it is used | ||
| * let clock = new Date("2020-01-01").getTime(); | ||
| * setDefaultRefDate(fakerCore, () => { | ||
| * clock += 1000; // +1s | ||
| * return new Date(clock); | ||
| * }); | ||
| * | ||
| * getDefaultRefDate(fakerCore) // 2020-01-01T00:00:01Z | ||
| * getDefaultRefDate(fakerCore) // 2020-01-01T00:00:02Z | ||
|
ST-DDT marked this conversation as resolved.
|
||
| * | ||
| * @since 10.5.0 | ||
| */ | ||
| export function getDefaultRefDate(fakerCore: FakerCore): Date { | ||
| return fakerCore.config.defaultRefDate?.() ?? DEFAULT_REF_DATE_SOURCE(); | ||
| } | ||
|
xDivisionByZerox marked this conversation as 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,11 @@ | ||
| import type { ModuleRegistry } from '../registry.types'; | ||
| import { getDefaultRefDate } from './get-default-ref-date'; | ||
| import { setDefaultRefDate } from './set-default-ref-date'; | ||
|
|
||
| /** | ||
| * Registry module containing all standalone utility functions for the Faker library. | ||
| */ | ||
| export const utilsModule = { | ||
| getDefaultRefDate, | ||
| setDefaultRefDate, | ||
| } as const satisfies ModuleRegistry; |
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,46 @@ | ||
| import type { FakerCore } from '../core'; | ||
|
|
||
| /** | ||
| * Sets the `refDate` source to use if no `refDate` date is passed to the date methods. | ||
| * | ||
| * @param fakerCore The FakerCore instance to use. | ||
| * @param dateOrSource The function or the static value used to generate the `refDate` date instance. | ||
| * The function must return a new valid `Date` instance for every call. | ||
| * Defaults to `() => new Date()`. | ||
| * | ||
| * @see [Reproducible Results](https://fakerjs.dev/guide/usage.html#reproducible-results) | ||
| * @see faker.seed(): For generating reproducible values. | ||
| * | ||
| * @example | ||
| * fakerCore.randomizer.seed(1234); | ||
| * | ||
| * // Default behavior | ||
| * // setDefaultRefDate(fakerCore); | ||
| * past(fakerCore); // Changes based on the current date/time | ||
| * | ||
| * // Use a static ref date | ||
| * setDefaultRefDate(fakerCore, new Date('2020-01-01')); | ||
| * past(fakerCore); // Reproducible '2019-07-03T08:27:58.118Z' | ||
|
ST-DDT marked this conversation as resolved.
|
||
| * | ||
| * // Use a ref date that changes every time it is used | ||
| * let clock = new Date("2020-01-01").getTime(); | ||
| * setDefaultRefDate(fakerCore, () => { | ||
| * clock += 1000; // +1s | ||
| * return new Date(clock); | ||
| * }); | ||
| * | ||
| * getDefaultRefDate(fakerCore) // 2020-01-01T00:00:01Z | ||
| * getDefaultRefDate(fakerCore) // 2020-01-01T00:00:02Z | ||
| * | ||
| * @since 10.5.0 | ||
| */ | ||
| export function setDefaultRefDate( | ||
| fakerCore: FakerCore, | ||
| dateOrSource: string | Date | number | (() => Date) = () => new Date() | ||
| ): void { | ||
| if (typeof dateOrSource === 'function') { | ||
| fakerCore.config.defaultRefDate = dateOrSource; | ||
| } else { | ||
| fakerCore.config.defaultRefDate = () => new Date(dateOrSource); | ||
| } | ||
| } | ||
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.