Skip to content

Commit 0e099a1

Browse files
ST-DDTejchengxDivisionByZeroxmatthewmayerShinigami92
authored
feat(person): sexType can return 'generic' (#3259)
Co-authored-by: Eric Cheng <[email protected]> Co-authored-by: xDivisionByZerox <[email protected]> Co-authored-by: Matt Mayer <[email protected]> Co-authored-by: Shinigami <[email protected]>
1 parent 8f5954a commit 0e099a1

File tree

8 files changed

+328
-151
lines changed

8 files changed

+328
-151
lines changed

src/definitions/person.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
import type { LocaleEntry } from './definitions';
22

3+
/**
4+
* Entries that are dependent on a person's sex.
5+
*/
36
export type PersonEntryDefinition<T> =
47
| {
8+
/**
9+
* Values that are primarily attributable to only females.
10+
*/
11+
female: T[];
12+
/**
13+
* Values that cannot clearly be attributed to a specific sex or are used for both sexes.
14+
*/
515
generic?: T[];
16+
/**
17+
* Values that are primarily attributable to only males.
18+
*/
619
male: T[];
7-
female: T[];
820
}
921
| {
22+
female?: never;
23+
/**
24+
* Values that cannot clearly be attributed to a specific sex or are used for both sexes.
25+
*/
1026
generic: T[];
1127
male?: never;
12-
female?: never;
1328
};
1429

1530
type SimplePersonEntryDefinition = PersonEntryDefinition<string>;

src/modules/image/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class ImageModule extends ModuleBase {
6060
* The image URLs are served via the JSDelivr CDN and subject to their [terms of use](https://www.jsdelivr.com/terms).
6161
*
6262
* @param options Options for generating an AI avatar.
63-
* @param options.sex The sex of the person for the avatar. Can be `'female'` or `'male'`. If not provided, defaults to a random selection.
63+
* @param options.sex The sex of the person for the avatar. Can be `'female'` or `'male'`. If not provided or `'generic'`, defaults to a random selection.
6464
* @param options.size The size of the image. Can be `512`, `256`, `128`, `64` or `32`. If not provided, defaults to `512`.
6565
*
6666
* @example
@@ -73,7 +73,7 @@ export class ImageModule extends ModuleBase {
7373
options: {
7474
/**
7575
* The sex of the person for the avatar.
76-
* Can be `'female'` or `'male'`.
76+
* Can be `'female'` or `'male'`. `'generic'` uses a random selection.
7777
*
7878
* @default faker.person.sexType()
7979
*/
@@ -87,7 +87,13 @@ export class ImageModule extends ModuleBase {
8787
size?: 512 | 256 | 128 | 64 | 32;
8888
} = {}
8989
): string {
90-
const { sex = this.faker.person.sexType(), size = 512 } = options;
90+
const { size = 512 } = options;
91+
let { sex = this.faker.person.sexType() } = options;
92+
93+
if (sex === 'generic') {
94+
sex = this.faker.person.sexType();
95+
}
96+
9197
const baseURL =
9298
'https://cdn.jsdelivr.net/gh/faker-js/assets-person-portrait';
9399
return `${baseURL}/${sex}/${size}/${this.faker.number.int({ min: 0, max: 99 })}.jpg`;

src/modules/person/index.ts

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import type { Faker } from '../..';
22
import type { PersonEntryDefinition } from '../../definitions/person';
33
import { ModuleBase } from '../../internal/module-base';
44

5+
/**
6+
* The enum for values corresponding to a person's sex.
7+
*/
58
export enum Sex {
9+
/**
10+
* Is used for values that are primarily attributable to only females.
11+
*/
612
Female = 'female',
13+
/**
14+
* Is used for values that cannot clearly be attributed to a specific sex or are used for both sexes.
15+
*/
16+
Generic = 'generic',
17+
/**
18+
* Is used for values that are primarily attributable to only males.
19+
*/
720
Male = 'male',
821
}
922

23+
/**
24+
* The parameter type for values corresponding to a person's sex.
25+
*/
1026
export type SexType = `${Sex}`;
1127

1228
/**
@@ -20,30 +36,48 @@ export type SexType = `${Sex}`;
2036
*/
2137
function selectDefinition<T>(
2238
faker: Faker,
23-
sex: SexType | undefined,
39+
sex: SexType = faker.person.sexType(),
2440
personEntry: PersonEntryDefinition<T>
2541
): T[] {
2642
const { generic, female, male } = personEntry;
27-
switch (sex) {
28-
case Sex.Female: {
29-
return female ?? generic;
30-
}
3143

32-
case Sex.Male: {
33-
return male ?? generic;
34-
}
44+
if (sex === 'generic') {
45+
return (
46+
generic ??
47+
faker.helpers.arrayElement([female, male]) ??
48+
// The last statement should never happen at run time. At this point in time,
49+
// the entry will satisfy at least (generic || (female && male)).
50+
// TS is not able to infer the type correctly.
51+
[]
52+
);
53+
}
3554

36-
default: {
37-
return (
38-
generic ??
39-
faker.helpers.arrayElement([female, male]) ??
40-
// The last statement should never happen at run time. At this point in time,
41-
// the entry will satisfy at least (generic || (female && male)).
42-
// TS is not able to infer the type correctly.
43-
[]
44-
);
55+
const binary = sex === 'female' ? female : male;
56+
57+
if (binary != null) {
58+
if (generic != null) {
59+
return faker.helpers.weightedArrayElement([
60+
{
61+
weight: 3 * Math.sqrt(binary.length),
62+
value: binary,
63+
},
64+
{
65+
weight: Math.sqrt(generic.length),
66+
value: generic,
67+
},
68+
]);
4569
}
70+
71+
return binary;
4672
}
73+
74+
return (
75+
generic ??
76+
// The last statement should never happen at run time. At this point in time,
77+
// the entry will satisfy at least (generic || (female && male)).
78+
// TS is not able to infer the type correctly.
79+
[]
80+
);
4781
}
4882

4983
/**
@@ -238,16 +272,38 @@ export class PersonModule extends ModuleBase {
238272
/**
239273
* Returns a random sex type. The `SexType` is intended to be used in parameters and conditions.
240274
*
275+
* @param options The optional options object.
276+
* @param options.includeGeneric Whether `'generic'` should be included in the potential outputs.
277+
* If `false`, this method only returns `'female'` and `'male'`.
278+
* Default is `false`.
279+
*
241280
* @see faker.person.gender(): For generating a gender related value in forms.
242281
* @see faker.person.sex(): For generating a binary-gender value in forms.
243282
*
244283
* @example
245284
* faker.person.sexType() // Sex.Female
285+
* faker.person.sexType({ includeGeneric: true }) // Sex.Generic
246286
*
247287
* @since 8.0.0
248288
*/
249-
sexType(): SexType {
250-
return this.faker.helpers.enumValue(Sex);
289+
sexType(
290+
options: {
291+
/**
292+
* Whether `'generic'` should be included in the potential outputs.
293+
* If `false`, this method only returns `'female'` and `'male'`.
294+
*
295+
* @default false
296+
*/
297+
includeGeneric?: boolean;
298+
} = {}
299+
): SexType {
300+
const { includeGeneric = false } = options;
301+
302+
if (includeGeneric) {
303+
return this.faker.helpers.enumValue(Sex);
304+
}
305+
306+
return this.faker.helpers.arrayElement([Sex.Female, Sex.Male]);
251307
}
252308

253309
/**

test/modules/__snapshots__/git.spec.ts.snap

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ exports[`git > 42 > commitDate > with only string refDate 1`] = `"Tue Dec 31 08:
1010

1111
exports[`git > 42 > commitEntry > with only Date refDate 1`] = `
1212
"commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92
13-
Author: Jerome Vandervort <Jerome_Vandervort@yahoo.com>
14-
Date: Tue Dec 31 14:20:57 2019 +1100
13+
Author: Suzanne.Hahn <Suzanne_Hahn92@yahoo.com>
14+
Date: Tue Dec 31 04:42:12 2019 -1000
1515
16-
    bypass bluetooth application
16+
    copy haptic card
1717
"
1818
`;
1919

2020
exports[`git > 42 > commitEntry > with only number refDate 1`] = `
2121
"commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92
22-
Author: Jerome Vandervort <Jerome_Vandervort@yahoo.com>
23-
Date: Tue Dec 31 14:20:57 2019 +1100
22+
Author: Suzanne.Hahn <Suzanne_Hahn92@yahoo.com>
23+
Date: Tue Dec 31 04:42:12 2019 -1000
2424
25-
    bypass bluetooth application
25+
    copy haptic card
2626
"
2727
`;
2828

2929
exports[`git > 42 > commitEntry > with only string refDate 1`] = `
3030
"commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92
31-
Author: Jerome Vandervort <Jerome_Vandervort@yahoo.com>
32-
Date: Tue Dec 31 14:20:57 2019 +1100
31+
Author: Suzanne.Hahn <Suzanne_Hahn92@yahoo.com>
32+
Date: Tue Dec 31 04:42:12 2019 -1000
3333
34-
    bypass bluetooth application
34+
    copy haptic card
3535
"
3636
`;
3737

@@ -53,28 +53,28 @@ exports[`git > 1211 > commitDate > with only string refDate 1`] = `"Tue Dec 31 2
5353

5454
exports[`git > 1211 > commitEntry > with only Date refDate 1`] = `
5555
"commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa
56-
Author: Deion Durgan <Deion.Durgan@yahoo.com>
57-
Date: Tue Dec 31 11:08:15 2019 -0600
56+
Author: Debbie.Kshlerin70 <Debbie.Kshlerin@hotmail.com>
57+
Date: Tue Dec 31 20:55:12 2019 -0900
5858
59-
    override solid state array
59+
    reboot multi-byte feed
6060
"
6161
`;
6262

6363
exports[`git > 1211 > commitEntry > with only number refDate 1`] = `
6464
"commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa
65-
Author: Deion Durgan <Deion.Durgan@yahoo.com>
66-
Date: Tue Dec 31 11:08:15 2019 -0600
65+
Author: Debbie.Kshlerin70 <Debbie.Kshlerin@hotmail.com>
66+
Date: Tue Dec 31 20:55:12 2019 -0900
6767
68-
    override solid state array
68+
    reboot multi-byte feed
6969
"
7070
`;
7171

7272
exports[`git > 1211 > commitEntry > with only string refDate 1`] = `
7373
"commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa
74-
Author: Deion Durgan <Deion.Durgan@yahoo.com>
75-
Date: Tue Dec 31 11:08:15 2019 -0600
74+
Author: Debbie.Kshlerin70 <Debbie.Kshlerin@hotmail.com>
75+
Date: Tue Dec 31 20:55:12 2019 -0900
7676
77-
    override solid state array
77+
    reboot multi-byte feed
7878
"
7979
`;
8080

@@ -96,28 +96,28 @@ exports[`git > 1337 > commitDate > with only string refDate 1`] = `"Tue Dec 31 0
9696

9797
exports[`git > 1337 > commitEntry > with only Date refDate 1`] = `
9898
"commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328
99-
Author: Matt_Hills <Matt_Hills99@hotmail.com>
100-
Date: Tue Dec 31 00:20:42 2019 -0900
99+
Author: Gene_Heller <Gene.Heller1@yahoo.com>
100+
Date: Tue Dec 31 22:32:07 2019 +1200
101101
102-
    synthesize wireless hard drive
102+
    index haptic pixel
103103
"
104104
`;
105105

106106
exports[`git > 1337 > commitEntry > with only number refDate 1`] = `
107107
"commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328
108-
Author: Matt_Hills <Matt_Hills99@hotmail.com>
109-
Date: Tue Dec 31 00:20:42 2019 -0900
108+
Author: Gene_Heller <Gene.Heller1@yahoo.com>
109+
Date: Tue Dec 31 22:32:07 2019 +1200
110110
111-
    synthesize wireless hard drive
111+
    index haptic pixel
112112
"
113113
`;
114114

115115
exports[`git > 1337 > commitEntry > with only string refDate 1`] = `
116116
"commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328
117-
Author: Matt_Hills <Matt_Hills99@hotmail.com>
118-
Date: Tue Dec 31 00:20:42 2019 -0900
117+
Author: Gene_Heller <Gene.Heller1@yahoo.com>
118+
Date: Tue Dec 31 22:32:07 2019 +1200
119119
120-
    synthesize wireless hard drive
120+
    index haptic pixel
121121
"
122122
`;
123123

0 commit comments

Comments
 (0)