faker
faker copied to clipboard
Incorrect behavior of `name.firstName()` function
Describe the bug
When using the faker.name.firstName() function with a non-standard locale and without passing a gender value to the function, the name is still displayed in English.
Reproduction
For example:
import { faker } from '@faker-js/faker';
faker.locale = 'ru'; // Set another locale
const incorrectFirstName = faker.name.firstName(); // Jack (incorrect)
const correctFirstName = faker.name.firstName(0); // Евгений (correct)
const correctFirstName2 = faker.name.firstName(1); // Жанна (correct)
demo: https://stackblitz.com/edit/faker-js-demo-6fb7x2?file=index.ts
Additional Info
No response
Yeah, thx, we already know that the name functions are really buggy :slightly_frowning_face: We will target this as one of the top prios when starting working on v6.1
thanks, maybe there is a problem in the code here: https://github.com/faker-js/faker/blob/03c3d1156312153428f2db5649b24dd5fc046d43/src/name.ts#L38-L65 at least it differs from what is in the lastName and middleName functions, which work correctly
I mean... that is quite the whole function except of 2-3 lines :wink: Maybe also have a look into https://github.com/faker-js/faker/pull/440
Needs re-evaluation after #644 was merged
Now displays Deprecation Warning
Yes, that is freshly implemented by us 🙂
Please do not use 0
or 1
as parameter, but 'male'
or 'female'
Yes, that is freshly implemented by us 🙂 Please do not use 0 or 1 as parameter, but 'male' or 'female'
yes, didn't notice It turns out the bug is still there
This issue is more complex then it looks like.
The basics:
-
female
is defined inru
-
male
is defined inru
-
generic
is defined inen
(fallback language) - If
generic
is not defined, we will usefemale
ormale
randomly as fallback.
The possible solutions:
- Create/Configure a faker instance without the
en
fallback (Suggested workaround for now) - Check whether
generic
is defined in the same language asfemale
andmale
, if not treat it as not defined
- This cannot be done via
faker.locales[faker.locale].name.xyz
because we wish to add support for multiple fallback languages in v7 (en_AU_ocker
->en_AU
->en
). Thus we would have to add a new "trace" property similar to definitions, that returns the source locale of a propertyfaker.trace.name.firstname => 'en'
- Create/Generate the
generic
source file automatically, if it is missing (pnpm run generate:locales
)
The generic
file may contain entries for non binary genders.
All name
methods (prefix
, suffix
, firstname
, middlename
, lastname
, ...) should behave the same in this regard.
yes, it looks like when specifying localeFallback in ru, the code works correctly https://stackblitz.com/edit/faker-js-demo-hxvbec?file=index.ts although this is more like a temporary fix, and should not be taken as a full solution to the problem
If I understand correctly, the third option (indicate male and female names (surnames, patronymics) in the general array) is acceptable
Team decision
Change data-structure:
name.female_first_name ->person.first_name.female
Example:
type Gendered = {
female: string[]
male: string[]
generic: string[]
};
Maybe I'm missing somethng, but isn't the simplest solution to add a first_name.ts file to locales which are missing one which simply concatenates the existing male_first_name and female_first_name lists?
Had a go at this in #1610
If you add a new locale or change the locale config (for most of the parts in person), then you will encounter the bug again. Changing the structure prevents that because then you have to either configure the entire block correctly or you will get compile time errors. You can either not define anything, define only generic, define only male and female or define all.
The effect of the bug is fixed. The implementation/structure should still be adjusted accordingly.
Further changes tracked via #1677