diff --git a/src/modules/name/index.ts b/src/modules/name/index.ts index 4b7e75d7375..07a4164d7fa 100644 --- a/src/modules/name/index.ts +++ b/src/modules/name/index.ts @@ -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 @@ -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), { diff --git a/test/name.spec.ts b/test/name.spec.ts index f0f216c3d1b..c7baf5a9283 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -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 = [ @@ -251,7 +251,7 @@ 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) { @@ -259,7 +259,7 @@ describe('name', () => { } }); - 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 = [ @@ -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) { @@ -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) { @@ -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) {