Skip to content

Commit

Permalink
refactor(name.fullName): arguments as object
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Jul 6, 2022
1 parent 06976b8 commit 0af76ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/modules/name/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,16 @@ export class Name {
since: '7.4',
until: '8.0',
});
return this.fullName(firstName, lastName, gender);
return this.fullName({ firstName, lastName, gender });
}

/**
* Generates a random full name.
*
* @param firstName The optional first name to use. If not specified a random one will be chosen.
* @param lastName The optional last name to use. If not specified a random one will be chosen.
* @param gender The optional gender to use.
* @param options An options object. Defaults to `{}`.
* @param options.firstName The optional first name to use. If not specified a random one will be chosen.
* @param options.lastName The optional last name to use. If not specified a random one will be chosen.
* @param options.gender The optional gender to use.
* Can be either `'female'` or `'male'`.
*
* @example
Expand All @@ -177,12 +178,18 @@ export class Name {
* faker.name.fullName(undefined, 'Beer') // 'Mr. Alfonso Beer'
* faker.name.fullName(undefined, undefined, 'male') // 'Fernando Schaefer'
*/
fullName(firstName?: string, lastName?: string, gender?: GenderType): string {
const normalizedGender: GenderType =
gender ?? this.faker.helpers.arrayElement(['female', 'male']);

firstName = firstName || this.firstName(normalizedGender);
lastName = lastName || this.lastName(normalizedGender);
fullName(
options: {
firstName?: string;
lastName?: string;
gender?: GenderType;
} = {}
): string {
const {
gender = this.faker.helpers.arrayElement(['female', 'male']),
firstName = this.firstName(gender),
lastName = this.lastName(gender),
} = options;

const nameParts: string[] = [];
const prefix = this.faker.helpers.maybe(() => this.prefix(gender), {
Expand Down
24 changes: 14 additions & 10 deletions test/name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('name', () => {
expect(fullName).toContain(' ');
});

it('should return a female gender-specific name with firstName and lastName', () => {
it('should return a female gender-specific name without firstName and lastName', () => {
faker.locale = 'mk';

const female_specific = [
Expand All @@ -251,15 +251,15 @@ describe('name', () => {
...faker.definitions.name.suffix,
];

const fullName = faker.name.fullName(undefined, undefined, 'female');
const fullName = faker.name.fullName({ gender: 'female' });

const parts = fullName.split(' ');
for (const part of parts) {
expect(female_specific).toContain(part);
}
});

it('should return a male gender-specific name with firstName and lastName', () => {
it('should return a male gender-specific name without firstName and lastName', () => {
faker.locale = 'mk';

const male_specific = [
Expand All @@ -269,7 +269,7 @@ describe('name', () => {
...faker.definitions.name.suffix,
];

const fullName = faker.name.fullName(undefined, undefined, 'male');
const fullName = faker.name.fullName({ gender: 'male' });

const parts = fullName.split(' ');
for (const part of parts) {
Expand All @@ -287,11 +287,11 @@ describe('name', () => {
...faker.definitions.name.suffix,
];

const fullName = faker.name.fullName(
'firstName',
'lastName',
'female'
);
const fullName = faker.name.fullName({
firstName: 'firstName',
lastName: 'lastName',
gender: 'female',
});

const parts = fullName.split(' ');
for (const part of parts) {
Expand All @@ -309,7 +309,11 @@ describe('name', () => {
...faker.definitions.name.suffix,
];

const fullName = faker.name.fullName('firstName', 'lastName', 'male');
const fullName = faker.name.fullName({
firstName: 'firstName',
lastName: 'lastName',
gender: 'male',
});

const parts = fullName.split(' ');
for (const part of parts) {
Expand Down

0 comments on commit 0af76ac

Please sign in to comment.