-
-
Notifications
You must be signed in to change notification settings - Fork 919
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
feat: introduce locale proxy #2004
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## next #2004 +/- ##
==========================================
- Coverage 99.59% 99.59% -0.01%
==========================================
Files 2540 2541 +1
Lines 242706 242800 +94
Branches 1299 1317 +18
==========================================
+ Hits 241723 241815 +92
- Misses 956 958 +2
Partials 27 27
|
Soft blocked by #2005 |
Suppose we decide some definitions are not wanted for What would it be replaced by? Completely remove the file? Or
Do we intend there for there to be a semantic difference between a missing definition and an empty array? |
I think the
Yes. nullish => Missing. [] => not applicable Lines 70 to 80 in 109ef31
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should document this in migration guide as a breaking change, something like this:
Attempting to access a method where Faker has no data will now throw a FakerError
instead of returning undefined
. Since the majority of methods have data in at least one fallback language (e.g. English), this error is likely to only happen in two situations
- Accessing data which is not applicable for the locale, for example Chinese does not use prefixes in names:
import { faker, fakerZH_CN } from '@faker-js/faker';
faker.name.prefix() // 'Mr'
fakerZH_CN.person.prefix() // throws a FakerError
- When using a custom Faker instance rather than one of the prebuilt instances.
import { Faker, fakerES, es } from '@faker-js/faker';
const fakerES_nofallbacks = new Faker({
locale: [es],
});
fakerES.music.songName() // 'I Want to Hold Your Hand' (fallback from en)
fakerES_nofallbacks.music.songName() // throws a FakerError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there should be merged some other PRs before this to reduce the diff
Maybe mark it as draft or label on hold or whatever, so reviewers are notified again when this PR is ready again
Blocked by #2078 |
Ready to review. |
Adds #893
Changes
faker.definitions
to a proxy that throws iffaker.definitions.category?.entry
is missing.faker.rawDefinitions
to access optional dataTypes
faker.definitions.person.first_name[0]: string
faker.rawDefinitions.person?.first_name?.[0]: string | undefined
Usage
faker.helpers.arrayElement(faker.definitions.person.first_name)
faker.helpers.arrayElement(faker.rawDefinitions.person?.first_name ?? ['alternatives'])