-
-
Notifications
You must be signed in to change notification settings - Fork 977
refactor(person): refine usage of PersonEntryDefinitions #3259
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
base: next
Are you sure you want to change the base?
Changes from all commits
1d02393
e5a397c
5989b95
926c959
ffaafd3
23e8f40
cdfad57
b61595c
3b60f8a
9b28f06
49cb2d7
fb732cc
69bcc13
caf6f65
f95cc4d
2a62e55
299bdaa
f20b83c
f79aa7e
c6836c9
612d6b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,27 @@ | |
import type { PersonEntryDefinition } from '../../definitions/person'; | ||
import { ModuleBase } from '../../internal/module-base'; | ||
|
||
/** | ||
* The enum for values corresponding to a person's sex. | ||
*/ | ||
export enum Sex { | ||
/** | ||
* Is used for values that are primarily attributable to only females. | ||
*/ | ||
Female = 'female', | ||
/** | ||
* Is used for values that cannot clearly be attributed to a specific sex or are used for both sexes. | ||
*/ | ||
Generic = 'generic', | ||
/** | ||
* Is used for values that are primarily attributable to only males. | ||
*/ | ||
Male = 'male', | ||
} | ||
|
||
/** | ||
* The parameter type for values corresponding to a person's sex. | ||
*/ | ||
export type SexType = `${Sex}`; | ||
|
||
/** | ||
|
@@ -20,30 +36,48 @@ | |
*/ | ||
function selectDefinition<T>( | ||
faker: Faker, | ||
sex: SexType | undefined, | ||
sex: SexType = faker.person.sexType(), | ||
personEntry: PersonEntryDefinition<T> | ||
): T[] { | ||
const { generic, female, male } = personEntry; | ||
switch (sex) { | ||
case Sex.Female: { | ||
return female ?? generic; | ||
} | ||
|
||
case Sex.Male: { | ||
return male ?? generic; | ||
} | ||
if (sex === 'generic') { | ||
return ( | ||
generic ?? | ||
faker.helpers.arrayElement([female, male]) ?? | ||
// The last statement should never happen at run time. At this point in time, | ||
// the entry will satisfy at least (generic || (female && male)). | ||
// TS is not able to infer the type correctly. | ||
[] | ||
); | ||
} | ||
|
||
default: { | ||
return ( | ||
generic ?? | ||
faker.helpers.arrayElement([female, male]) ?? | ||
// The last statement should never happen at run time. At this point in time, | ||
// the entry will satisfy at least (generic || (female && male)). | ||
// TS is not able to infer the type correctly. | ||
[] | ||
); | ||
const binary = sex === 'female' ? female : male; | ||
|
||
if (binary != null) { | ||
if (generic != null) { | ||
return faker.helpers.weightedArrayElement([ | ||
{ | ||
weight: 3 * Math.sqrt(binary.length), | ||
value: binary, | ||
}, | ||
{ | ||
weight: Math.sqrt(generic.length), | ||
value: generic, | ||
}, | ||
]); | ||
} | ||
|
||
return binary; | ||
} | ||
|
||
return ( | ||
generic ?? | ||
// The last statement should never happen at run time. At this point in time, | ||
// the entry will satisfy at least (generic || (female && male)). | ||
// TS is not able to infer the type correctly. | ||
[] | ||
); | ||
Comment on lines
+74
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "catch" case here, that basically duplicates the generic part, could be prevented by using a switch with an explicit handle for |
||
} | ||
|
||
/** | ||
|
@@ -238,16 +272,38 @@ | |
/** | ||
* Returns a random sex type. The `SexType` is intended to be used in parameters and conditions. | ||
* | ||
* @param options The optional options object. | ||
* @param options.includeGeneric Whether `'generic'` should be included in the potential outputs. | ||
* If `false`, this method only returns `'female'` and `'male'`. | ||
* Default is `false`. | ||
* | ||
* @see faker.person.gender(): For generating a gender related value in forms. | ||
* @see faker.person.sex(): For generating a binary-gender value in forms. | ||
* | ||
* @example | ||
* faker.person.sexType() // Sex.Female | ||
* faker.person.sexType({ includeGeneric: true }) // Sex.Generic | ||
* | ||
* @since 8.0.0 | ||
*/ | ||
sexType(): SexType { | ||
return this.faker.helpers.enumValue(Sex); | ||
sexType( | ||
options: { | ||
/** | ||
* Whether `'generic'` should be included in the potential outputs. | ||
* If `false`, this method only returns `'female'` and `'male'`. | ||
* | ||
* @default false | ||
*/ | ||
includeGeneric?: boolean; | ||
} = {} | ||
): SexType { | ||
const { includeGeneric = false } = options; | ||
|
||
if (includeGeneric) { | ||
return this.faker.helpers.enumValue(Sex); | ||
} | ||
|
||
return this.faker.helpers.arrayElement([Sex.Female, Sex.Male]); | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.