-
-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathindex.ts
401 lines (369 loc) · 11.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
export enum Sex {
Female = 'female',
Male = 'male',
}
export type SexType = `${Sex}`;
/**
* Select a definition based on given sex.
*
* @param faker Faker instance.
* @param elementSelectorFn The method used to select the actual element.
* @param sex Sex.
* @param param2 Definitions.
* @param param2.generic Non-sex definitions.
* @param param2.female Female definitions.
* @param param2.male Male definitions.
*
* @returns Definition based on given sex.
*/
function selectDefinition<T>(
faker: Faker,
elementSelectorFn: (values: T[]) => string,
sex: SexType | undefined,
// TODO @Shinigami92 2022-03-21: Remove fallback empty object when `strict: true`
{ generic, female, male }: { generic?: T[]; female?: T[]; male?: T[] } = {}
): string {
let values: T[] | undefined;
switch (sex) {
case Sex.Female:
values = female;
break;
case Sex.Male:
values = male;
break;
default:
values = generic;
break;
}
if (values == null) {
if (female != null && male != null) {
values = faker.helpers.arrayElement([female, male]);
} else {
values = generic;
}
}
return elementSelectorFn(values);
}
/**
* Module to generate people's personal information such as names and job titles. Prior to Faker 8.0.0, this module was known as `faker.name`.
*
* ### Overview
*
* To generate a full name, use [`fullName`](https://fakerjs.dev/api/person.html#fullname). Note that this is not the same as simply concatenating [`firstName`](https://fakerjs.dev/api/person.html#firstname) and [`lastName`](https://fakerjs.dev/api/person.html#lastname), as the full name may contain a prefix, suffix, or both. Additionally, different supported locales will have differing name patterns. For example, the last name may appear before the first name, or there may be a double or hyphenated first or last name.
*
* You can also generate the parts of a name separately, using [`prefix`](https://fakerjs.dev/api/person.html#prefix), [`firstName`](https://fakerjs.dev/api/person.html#firstname), [`middleName`](https://fakerjs.dev/api/person.html#middlename), [`lastName`](https://fakerjs.dev/api/person.html#lastname), and [`suffix`](https://fakerjs.dev/api/person.html#suffix). Not all locales support all of these parts.
*
* Many of the methods in this module can optionally choose either female, male or mixed names.
*
* Job-related data is also available. To generate a job title, use [`jobTitle`](https://fakerjs.dev/api/person.html#jobtitle).
*
* This module can also generate other personal information which might appear in user profiles, such as [`gender`](https://fakerjs.dev/api/person.html#gender), [`zodiacSign`](https://fakerjs.dev/api/person.html#zodiacsign), and [`bio`](https://fakerjs.dev/api/person.html#bio).
*
* ### Related modules
*
* For personal contact information like phone numbers and email addresses, see the [`faker.phone`](https://fakerjs.dev/api/phone.html) and [`faker.internet`](https://fakerjs.dev/api/internet.html) modules.
*/
export class PersonModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}
/**
* Returns a random first name.
*
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.person.firstName() // 'Antwan'
* faker.person.firstName('female') // 'Victoria'
* faker.person.firstName('male') // 'Tom'
*
* @since 8.0.0
*/
firstName(sex?: SexType): string {
const { first_name, female_first_name, male_first_name } =
this.faker.rawDefinitions.person ?? {};
return selectDefinition(this.faker, this.faker.helpers.arrayElement, sex, {
generic: first_name,
female: female_first_name,
male: male_first_name,
});
}
/**
* Returns a random last name.
*
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.person.lastName() // 'Hauck'
* faker.person.lastName('female') // 'Grady'
* faker.person.lastName('male') // 'Barton'
*
* @since 8.0.0
*/
lastName(sex?: SexType): string {
const {
last_name,
female_last_name,
male_last_name,
last_name_pattern,
male_last_name_pattern,
female_last_name_pattern,
} = this.faker.rawDefinitions.person ?? {};
if (
last_name_pattern != null ||
male_last_name_pattern != null ||
female_last_name_pattern != null
) {
const pattern = selectDefinition(
this.faker,
this.faker.helpers.weightedArrayElement,
sex,
{
generic: last_name_pattern,
female: female_last_name_pattern,
male: male_last_name_pattern,
}
);
return this.faker.helpers.fake(pattern);
}
return selectDefinition(this.faker, this.faker.helpers.arrayElement, sex, {
generic: last_name,
female: female_last_name,
male: male_last_name,
});
}
/**
* Returns a random middle name.
*
* @param sex The optional sex to use.
* Can be either `'female'` or `'male'`.
*
* @example
* faker.person.middleName() // 'James'
* faker.person.middleName('female') // 'Eloise'
* faker.person.middleName('male') // 'Asher'
*
* @since 8.0.0
*/
middleName(sex?: SexType): string {
const { middle_name, female_middle_name, male_middle_name } =
this.faker.rawDefinitions.person ?? {};
return selectDefinition(this.faker, this.faker.helpers.arrayElement, sex, {
generic: middle_name,
female: female_middle_name,
male: male_middle_name,
});
}
/**
* Generates a random full 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.sex The optional sex to use. Can be either `'female'` or `'male'`.
*
* @example
* faker.person.fullName() // 'Allen Brown'
* faker.person.fullName({ firstName: 'Joann' }) // 'Joann Osinski'
* faker.person.fullName({ firstName: 'Marcella', sex: 'female' }) // 'Mrs. Marcella Huels'
* faker.person.fullName({ lastName: 'Beer' }) // 'Mr. Alfonso Beer'
* faker.person.fullName({ sex: 'male' }) // 'Fernando Schaefer'
*
* @since 8.0.0
*/
fullName(
options: {
/**
* The optional first name to use. If not specified a random one will be chosen.
*
* @default faker.person.firstName(sex)
*/
firstName?: string;
/**
* The optional last name to use. If not specified a random one will be chosen.
*
* @default faker.person.lastName(sex)
*/
lastName?: string;
/**
* The optional sex to use. Can be either `'female'` or `'male'`.
*
* @default faker.helpers.arrayElement(['female', 'male'])
*/
sex?: SexType;
} = {}
): string {
const {
sex = this.faker.helpers.arrayElement([Sex.Female, Sex.Male]),
firstName = this.firstName(sex),
lastName = this.lastName(sex),
} = options;
const fullNamePattern: string = this.faker.helpers.weightedArrayElement(
this.faker.definitions.person.name
);
const fullName = this.faker.helpers.mustache(fullNamePattern, {
'person.prefix': () => this.prefix(sex),
'person.firstName': () => firstName,
'person.middleName': () => this.middleName(sex),
'person.lastName': () => lastName,
'person.suffix': () => this.suffix(),
});
return fullName;
}
/**
* Returns a random gender.
*
* @see faker.person.sex() if you would like to generate binary-gender value
*
* @example
* faker.person.gender() // 'Trans*Man'
*
* @since 8.0.0
*/
gender(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.person.gender
);
}
/**
* Returns a random sex.
*
* Output of this method is localised, so it should not be used to fill the parameter `sex`
* available in some other modules for example `faker.person.firstName()`.
*
* @see faker.person.gender() if you would like to generate gender related values.
*
* @example
* faker.person.sex() // 'female'
*
* @since 8.0.0
*/
sex(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.person.sex);
}
/**
* Returns a random sex type.
*
* @example
* faker.person.sexType() // Sex.Female
*
* @since 8.0.0
*/
sexType(): SexType {
return this.faker.helpers.enumValue(Sex);
}
/**
* Returns a random short biography
*
* @example
* faker.person.bio() // 'oatmeal advocate, veteran 🐠'
*
* @since 8.0.0
*/
bio(): string {
const { bio_pattern } = this.faker.definitions.person;
return this.faker.helpers.fake(bio_pattern);
}
/**
* Returns a random person prefix.
*
* @param sex The optional sex to use. Can be either `'female'` or `'male'`.
*
* @example
* faker.person.prefix() // 'Miss'
* faker.person.prefix('female') // 'Ms.'
* faker.person.prefix('male') // 'Mr.'
*
* @since 8.0.0
*/
prefix(sex?: SexType): string {
const { prefix, female_prefix, male_prefix } =
this.faker.rawDefinitions.person ?? {};
return selectDefinition(this.faker, this.faker.helpers.arrayElement, sex, {
generic: prefix,
female: female_prefix,
male: male_prefix,
});
}
/**
* Returns a random person suffix.
*
* @example
* faker.person.suffix() // 'DDS'
*
* @since 8.0.0
*/
suffix(): string {
// TODO @Shinigami92 2022-03-21: Add female_suffix and male_suffix
return this.faker.helpers.arrayElement(
this.faker.definitions.person.suffix
);
}
/**
* Generates a random job title.
*
* @example
* faker.person.jobTitle() // 'Global Accounts Engineer'
*
* @since 8.0.0
*/
jobTitle(): string {
return `${this.jobDescriptor()} ${this.jobArea()} ${this.jobType()}`;
}
/**
* Generates a random job descriptor.
*
* @example
* faker.person.jobDescriptor() // 'Customer'
*
* @since 8.0.0
*/
jobDescriptor(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.person.title.descriptor
);
}
/**
* Generates a random job area.
*
* @example
* faker.person.jobArea() // 'Brand'
*
* @since 8.0.0
*/
jobArea(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.person.title.level
);
}
/**
* Generates a random job type.
*
* @example
* faker.person.jobType() // 'Assistant'
*
* @since 8.0.0
*/
jobType(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.person.title.job
);
}
/**
* Returns a random zodiac sign.
*
* @example
* faker.person.zodiacSign() // 'Pisces'
*
* @since 8.0.0
*/
zodiacSign(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.person.western_zodiac_sign
);
}
}