Skip to content
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(name): add full_name_pattern configuration #1211 #1216

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/locales/ja/name/full_name_pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default [
'{{last_name}} {{first_name}}',
'{{last_name}} {{male_first_name}}',
'{{last_name}} {{female_first_name}}',
];
2 changes: 2 additions & 0 deletions src/locales/ja/name/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import type { NameDefinitions } from '../../..';
import female_first_name from './female_first_name';
import first_name from './first_name';
import full_name_pattern from './full_name_pattern';
import last_name from './last_name';
import male_first_name from './male_first_name';
import name_ from './name';

const name: NameDefinitions = {
female_first_name,
first_name,
full_name_pattern,
last_name,
male_first_name,
name: name_,
Expand Down
6 changes: 5 additions & 1 deletion src/locales/ja/name/name.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export default ['{{name.last_name}} {{name.first_name}}'];
export default [
'{{name.last_name}} {{name.first_name}}',
'{{name.last_name}} {{name.male_first_name}}',
'{{name.last_name}} {{name.female_first_name}}',
];
46 changes: 39 additions & 7 deletions src/modules/name/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ export enum Gender {
male = 'male',
}

/**
* @deprecated Use SexType instead.
*/
export type GenderType = SexType;

export enum Sex {
Female = 'female',
Male = 'male',
}

export type SexType = `${Sex}`;
export type GenderType = 'female' | 'male';

/**
* Select a definition based on given sex.
Expand Down Expand Up @@ -229,7 +225,6 @@ export class Name {
if (prefix) {
nameParts.push(prefix);
}

nameParts.push(firstName);
nameParts.push(lastName);

Expand All @@ -240,8 +235,45 @@ export class Name {
if (suffix) {
nameParts.push(suffix);
}
const defaultName = nameParts.join(' ');

if (
!this.faker.definitions.name.name ||
this.faker.definitions.name.name.length === 0
) {
return defaultName;
}
if (gender || firstName || lastName) {
return defaultName;
}

const fullNamePattern = this.faker.helpers.arrayElement(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selectDefinition method at the top should do the same in a more readable way..

this.faker.definitions.name.name
);

const fullName = this.faker.helpers.mustache(fullNamePattern, {
'name.gender': () => this.gender(),
'name.binary_gender': () => this.gender(true),
'name.prefix': () => this.prefix(sex),
'name.female_prefix': () => this.prefix(sex),
'name.male_prefix': () => this.prefix(sex),
'name.first_name': () => (firstName ? firstName : this.firstName(sex)),
'name.female_first_name': () =>
firstName ? firstName : this.firstName(sex ? sex : 'female'),
'name.male_first_name': () =>
firstName ? firstName : this.firstName(sex ? sex : 'male'),
'name.middle_name': () => this.middleName(sex),
'name.female_middle_name': () => this.middleName(sex ? sex : 'female'),
'name.male_middle_name': () => this.middleName(sex ? sex : 'male'),
'name.last_name': () => (lastName ? lastName : this.lastName(sex)),
'name.female_last_name': () =>
lastName ? lastName : this.lastName(sex ? sex : 'female'),
'name.male_last_name': () =>
lastName ? lastName : this.lastName(sex ? sex : 'male'),
'name.suffix': () => (suffix ? suffix : this.suffix()),
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this, as this hard to maintain/extend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ST-DDT
Is this an implementation issue?
Or is it an idea problem?
Thank you.

Copy link
Member

@ST-DDT ST-DDT Sep 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be nice of you could at least remove/merge the male/female parts and then call it with the (pregenerated) gender option.
Or maybe, doing a faker.mustache with the method names, followed by a faker.fake with the locale keys.
Not sure what "feels" best.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ST-DDT
Please take a look at the following commit.
073140a


return nameParts.join(' ');
return fullName;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion test/name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ describe('name', () => {

it('should return a name with firstName and lastName', () => {
const fullName = faker.name.fullName();

expect(fullName).toBeTypeOf('string');
expect(fullName).toContain(' ');
});
Expand Down Expand Up @@ -327,6 +326,21 @@ describe('name', () => {
expect(male_specific).toContain(part);
}
});

it('If fullNamePattern is defined, it should be configured accordingly.', () => {
faker.locale = 'ja';

const fullName = faker.name.fullName();
const male_specific = [
...faker.definitions.name.female_first_name,
...faker.definitions.name.male_first_name,
...faker.definitions.name.last_name,
];
const parts = fullName.split(' ');
for (const part of parts) {
expect(male_specific).toContain(part);
}
});
});

describe('gender()', () => {
Expand Down