faker icon indicating copy to clipboard operation
faker copied to clipboard

Incorrect behavior of `name.firstName()` function

Open JoCat opened this issue 3 years ago • 10 comments

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

JoCat avatar Feb 23 '22 12:02 JoCat

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

Shinigami92 avatar Feb 23 '22 13:02 Shinigami92

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

JoCat avatar Feb 23 '22 13:02 JoCat

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

Shinigami92 avatar Feb 23 '22 13:02 Shinigami92

Needs re-evaluation after #644 was merged

Shinigami92 avatar Mar 23 '22 17:03 Shinigami92

Now displays Deprecation Warning image

JoCat avatar Mar 23 '22 18:03 JoCat

Yes, that is freshly implemented by us 🙂 Please do not use 0 or 1 as parameter, but 'male' or 'female'

Shinigami92 avatar Mar 23 '22 18:03 Shinigami92

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

image

JoCat avatar Mar 23 '22 21:03 JoCat

This issue is more complex then it looks like.

The basics:

  • female is defined in ru
  • male is defined in ru
  • generic is defined in en (fallback language)
  • If generic is not defined, we will use female or male randomly as fallback.

The possible solutions:

  1. Create/Configure a faker instance without the en fallback (Suggested workaround for now)
  2. Check whether generic is defined in the same language as female and male, 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 property faker.trace.name.firstname => 'en'
  1. 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.

ST-DDT avatar Mar 24 '22 11:03 ST-DDT

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

JoCat avatar Mar 24 '22 14:03 JoCat

Team decision

Change data-structure:

name.female_first_name ->person.first_name.female

Example:

type Gendered = {
  female: string[]
  male: string[]
  generic: string[]
};

ST-DDT avatar Sep 08 '22 16:09 ST-DDT

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

matthewmayer avatar Nov 27 '22 10:11 matthewmayer

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.

ST-DDT avatar Nov 27 '22 18:11 ST-DDT

The effect of the bug is fixed. The implementation/structure should still be adjusted accordingly.

ST-DDT avatar Dec 21 '22 01:12 ST-DDT

Further changes tracked via #1677

ST-DDT avatar Dec 21 '22 11:12 ST-DDT