aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorLeyla Jähnig <[email protected]>2022-06-18 16:48:32 +0200
committerGitHub <[email protected]>2022-06-18 14:48:32 +0000
commit045b8d60a984976b088d8bc28dc61798e9cad2bf (patch)
tree3734128a33f971a818aa0c68df6bc1fbaf89f50d /src/modules
parenta0d7191d4ec071fd815f31f3a91bef368d6cdb89 (diff)
downloadfaker-045b8d60a984976b088d8bc28dc61798e9cad2bf.tar.xz
faker-045b8d60a984976b088d8bc28dc61798e9cad2bf.zip
refactor(name.findName): allow prefix and suffix (#1080)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/name/index.ts38
1 files changed, 19 insertions, 19 deletions
diff --git a/src/modules/name/index.ts b/src/modules/name/index.ts
index f1d357e7..009b35fc 100644
--- a/src/modules/name/index.ts
+++ b/src/modules/name/index.ts
@@ -148,33 +148,33 @@ export class Name {
* faker.name.findName(undefined, undefined, 'male') // 'Fernando Schaefer'
*/
findName(firstName?: string, lastName?: string, gender?: GenderType): string {
- const variant = this.faker.datatype.number(8);
- let prefix = '';
- let suffix = '';
-
const normalizedGender: GenderType =
gender ?? this.faker.helpers.arrayElement(['female', 'male']);
firstName = firstName || this.firstName(normalizedGender);
lastName = lastName || this.lastName(normalizedGender);
- switch (variant) {
- // TODO @Shinigami92 2022-03-21: Add possibility to have a prefix together with a suffix
- case 0:
- prefix = this.prefix(gender);
- if (prefix) {
- return `${prefix} ${firstName} ${lastName}`;
- }
- // TODO @Shinigami92 2022-01-21: Not sure if this fallthrough is wanted
- // eslint-disable-next-line no-fallthrough
- case 1:
- suffix = this.suffix();
- if (suffix) {
- return `${firstName} ${lastName} ${suffix}`;
- }
+ const nameParts: string[] = [];
+ const prefix = this.faker.helpers.maybe(() => this.prefix(gender), {
+ probability: 0.125,
+ });
+ if (prefix) {
+ nameParts.push(prefix);
}
- return `${firstName} ${lastName}`;
+ nameParts.push(firstName);
+ nameParts.push(lastName);
+
+ const suffix = this.faker.helpers.maybe(() => this.suffix(), {
+ probability: 0.125,
+ });
+ if (suffix) {
+ nameParts.push(suffix);
+ }
+
+ const fullName = nameParts.join(' ');
+
+ return fullName;
}
/**