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

chore: rename Gender to Sex #1163

Merged
merged 13 commits into from
Aug 1, 2022
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export type {
NumberColorFormat,
StringColorFormat,
} from './modules/color';
export { Gender } from './modules/name';
export type { GenderType } from './modules/name';
hankucz marked this conversation as resolved.
Show resolved Hide resolved
hankucz marked this conversation as resolved.
Show resolved Hide resolved
export { Gender, Sex } from './modules/name';
export type { GenderType, SexType } from './modules/name';
export type { ChemicalElement, Unit } from './modules/science';
export { Faker };

Expand Down
106 changes: 66 additions & 40 deletions src/modules/name/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import type { Faker } from '../..';
import { deprecated } from '../../internal/deprecated';

// disabled until renamed to Sex
/**
* @deprecated Use Sex enum instead.
*/
export enum Gender {
// disabled until renamed to Sex
// eslint-disable-next-line @typescript-eslint/naming-convention
female = 'female',
// eslint-disable-next-line @typescript-eslint/naming-convention
male = 'male',
}

export type GenderType = 'female' | 'male';
/**
* @deprecated Use SexType instead.
*/
export type GenderType = SexType;

export enum Sex {
Female = 'female',
Male = 'male',
hankucz marked this conversation as resolved.
Show resolved Hide resolved
}

export type SexType = `${Sex}`;

/**
* Select a definition based on given gender.
* Select a definition based on given sex.
*
* @param faker Faker instance.
* @param gender Gender.
* @param sex Sex.
* @param param2 Definitions.
* @param param2.generic Non-gender definitions.
* @param param2.generic Non-sex definitions.
* @param param2.female Female definitions.
* @param param2.male Male definitions.
* @returns Definition based on given gender.
* @returns Definition based on given sex.
*/
function selectDefinition(
faker: Faker,
gender: GenderType | undefined,
sex: SexType | undefined,
// TODO @Shinigami92 2022-03-21: Remove fallback empty object when `strict: true`
{
generic,
Expand All @@ -33,13 +46,16 @@ function selectDefinition(
}: { generic?: string[]; female?: string[]; male?: string[] } = {}
) {
let values: string[] | undefined;
switch (gender) {
case 'female':

switch (sex) {
case Sex.Female:
pkuczynski marked this conversation as resolved.
Show resolved Hide resolved
values = female;
break;
case 'male':

hankucz marked this conversation as resolved.
Show resolved Hide resolved
case Sex.Male:
values = male;
break;

hankucz marked this conversation as resolved.
Show resolved Hide resolved
default:
values = generic;
break;
Expand Down Expand Up @@ -73,19 +89,19 @@ export class Name {
/**
* Returns a random first name.
*
* @param gender The optional gender to use.
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.name.firstName() // 'Antwan'
* faker.name.firstName('female') // 'Victoria'
* faker.name.firstName('male') // 'Tom'
*/
firstName(gender?: GenderType): string {
firstName(sex?: SexType): string {
const { first_name, female_first_name, male_first_name } =
this.faker.definitions.name;

return selectDefinition(this.faker, gender, {
return selectDefinition(this.faker, sex, {
generic: first_name,
female: female_first_name,
male: male_first_name,
Expand All @@ -95,19 +111,19 @@ export class Name {
/**
* Returns a random last name.
*
* @param gender The optional gender to use.
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.name.lastName() // 'Hauck'
* faker.name.lastName('female') // 'Grady'
* faker.name.lastName('male') // 'Barton'
*/
lastName(gender?: GenderType): string {
lastName(sex?: SexType): string {
const { last_name, female_last_name, male_last_name } =
this.faker.definitions.name;

return selectDefinition(this.faker, gender, {
return selectDefinition(this.faker, sex, {
generic: last_name,
female: female_last_name,
male: male_last_name,
Expand All @@ -117,19 +133,19 @@ export class Name {
/**
* Returns a random middle name.
*
* @param gender The optional gender to use.
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.name.middleName() // 'James'
* faker.name.middleName('female') // 'Eloise'
* faker.name.middleName('male') // 'Asher'
*/
middleName(gender?: GenderType): string {
middleName(sex?: SexType): string {
const { middle_name, female_middle_name, male_middle_name } =
this.faker.definitions.name;

return selectDefinition(this.faker, gender, {
return selectDefinition(this.faker, sex, {
generic: middle_name,
female: female_middle_name,
male: male_middle_name,
Expand All @@ -141,8 +157,7 @@ export class 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.
* Can be either `'female'` or `'male'`.
* @param sex The optional sex to use. Can be either `'female'` or `'male'`.
*
* @see faker.name.fullName()
*
Expand All @@ -155,14 +170,15 @@ export class Name {
*
* @deprecated Use faker.name.fullName() instead.
*/
findName(firstName?: string, lastName?: string, gender?: GenderType): string {
findName(firstName?: string, lastName?: string, sex?: SexType): string {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
deprecated({
deprecated: 'faker.name.findName()',
proposed: 'faker.name.fullName()',
since: '7.4',
until: '8.0',
});
return this.fullName({ firstName, lastName, gender });

hankucz marked this conversation as resolved.
Show resolved Hide resolved
return this.fullName({ firstName, lastName, sex });
}

/**
Expand All @@ -171,33 +187,45 @@ export class Name {
* @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'`.
* @param options.sex The optional sex to use. Can be either `'female'` or `'male'`.
* @param options.gender Deprecated. Use `sex` instead.
*
* @example
* faker.name.fullName() // 'Allen Brown'
* faker.name.fullName('Joann') // 'Joann Osinski'
* faker.name.fullName('Marcella', '', 'female') // 'Mrs. Marcella Huels'
* faker.name.fullName(undefined, 'Beer') // 'Mr. Alfonso Beer'
* faker.name.fullName(undefined, undefined, 'male') // 'Fernando Schaefer'
* faker.name.fullName({ firstName: 'Joann' }) // 'Joann Osinski'
* faker.name.fullName({ firstName: 'Marcella', sex: 'female' }) // 'Mrs. Marcella Huels'
* faker.name.fullName({ lastName: 'Beer' }) // 'Mr. Alfonso Beer'
* faker.name.fullName({ sex: 'male' }) // 'Fernando Schaefer'
*/
fullName(
options: {
firstName?: string;
lastName?: string;
gender?: GenderType;
sex?: SexType;
} = {}
): string {
const {
gender = this.faker.helpers.arrayElement(['female', 'male']),
firstName = this.firstName(gender),
lastName = this.lastName(gender),
gender,
sex = gender || this.faker.helpers.arrayElement([Sex.Female, Sex.Male]),
firstName = this.firstName(sex),
lastName = this.lastName(sex),
} = options;

if (gender) {
deprecated({
deprecated: `faker.name.fullName({ gender: '...' })`,
proposed: `faker.name.fullName({ sex: '...' })`,
since: '7.4',
until: '8.0',
});
}

const nameParts: string[] = [];
const prefix = this.faker.helpers.maybe(() => this.prefix(gender), {
const prefix = this.faker.helpers.maybe(() => this.prefix(sex), {
probability: 0.125,
});

if (prefix) {
nameParts.push(prefix);
}
Expand All @@ -208,13 +236,12 @@ export class Name {
const suffix = this.faker.helpers.maybe(() => this.suffix(), {
probability: 0.125,
});

if (suffix) {
nameParts.push(suffix);
}

const fullName = nameParts.join(' ');

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

/**
Expand All @@ -239,18 +266,17 @@ export class Name {
/**
* Returns a random name prefix.
*
* @param gender The optional gender to use.
* Can be either `'female'` or `'male'`.
* @param sex The optional sex to use. Can be either `'female'` or `'male'`.
*
* @example
* faker.name.prefix() // 'Miss'
* faker.name.prefix('female') // 'Ms.'
* faker.name.prefix('male') // 'Mr.'
*/
prefix(gender?: GenderType): string {
prefix(sex?: SexType): string {
const { prefix, female_prefix, male_prefix } = this.faker.definitions.name;

return selectDefinition(this.faker, gender, {
return selectDefinition(this.faker, sex, {
generic: prefix,
female: female_prefix,
male: male_prefix,
Expand Down
18 changes: 15 additions & 3 deletions test/__snapshots__/name.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ exports[`name > 42 > firstName > with gender 1`] = `"Glen"`;

exports[`name > 42 > fullName > noArgs 1`] = `"Sadie Wiegand"`;

exports[`name > 42 > fullName > with all 1`] = `"John Doe"`;
exports[`name > 42 > fullName > with all (gender) 1`] = `"John Doe"`;

exports[`name > 42 > fullName > with all (sex) 1`] = `"John Doe"`;

exports[`name > 42 > fullName > with firstName 1`] = `"John Schinner"`;

exports[`name > 42 > fullName > with gender 1`] = `"Melanie Schinner"`;

exports[`name > 42 > fullName > with lastName 1`] = `"Sadie Doe"`;

exports[`name > 42 > fullName > with sex 1`] = `"Melanie Schinner"`;

exports[`name > 42 > gender > noArgs 1`] = `"Gender nonconforming"`;

exports[`name > 42 > gender > with gender 1`] = `"Female"`;
Expand Down Expand Up @@ -64,14 +68,18 @@ exports[`name > 1211 > firstName > with gender 1`] = `"Percy"`;

exports[`name > 1211 > fullName > noArgs 1`] = `"Claude Trantow"`;

exports[`name > 1211 > fullName > with all 1`] = `"John Doe"`;
exports[`name > 1211 > fullName > with all (gender) 1`] = `"John Doe"`;

exports[`name > 1211 > fullName > with all (sex) 1`] = `"John Doe"`;

exports[`name > 1211 > fullName > with firstName 1`] = `"John Koch"`;

exports[`name > 1211 > fullName > with gender 1`] = `"Patti Koch"`;

exports[`name > 1211 > fullName > with lastName 1`] = `"Claude Doe"`;

exports[`name > 1211 > fullName > with sex 1`] = `"Patti Koch"`;

exports[`name > 1211 > gender > noArgs 1`] = `"Trigender"`;

exports[`name > 1211 > gender > with gender 1`] = `"Male"`;
Expand Down Expand Up @@ -114,14 +122,18 @@ exports[`name > 1337 > firstName > with gender 1`] = `"Ray"`;

exports[`name > 1337 > fullName > noArgs 1`] = `"Leona Cronin"`;

exports[`name > 1337 > fullName > with all 1`] = `"John Doe"`;
exports[`name > 1337 > fullName > with all (gender) 1`] = `"John Doe"`;

exports[`name > 1337 > fullName > with all (sex) 1`] = `"John Doe"`;

exports[`name > 1337 > fullName > with firstName 1`] = `"John Macejkovic"`;

exports[`name > 1337 > fullName > with gender 1`] = `"Esther Macejkovic"`;

exports[`name > 1337 > fullName > with lastName 1`] = `"Leona Doe"`;

exports[`name > 1337 > fullName > with sex 1`] = `"Esther Macejkovic"`;

exports[`name > 1337 > gender > noArgs 1`] = `"Demigender"`;

exports[`name > 1337 > gender > with gender 1`] = `"Female"`;
Expand Down
Loading