From 90c509ff77836421628152caeb203a4a78d9474c Mon Sep 17 00:00:00 2001 From: ganesh Date: Sat, 5 Feb 2022 17:55:22 -0500 Subject: [PATCH 1/3] fix: added curated names for default(en) locale fixes #373 --- src/locales/en/name/female_middle_name.ts | 212 ++++++++++++++++++++++ src/locales/en/name/index.ts | 6 + src/locales/en/name/male_middle_name.ts | 100 ++++++++++ src/locales/en/name/middle_name.ts | 64 +++++++ src/name.ts | 18 +- test/name.spec.ts | 17 +- 6 files changed, 412 insertions(+), 5 deletions(-) create mode 100644 src/locales/en/name/female_middle_name.ts create mode 100644 src/locales/en/name/male_middle_name.ts create mode 100644 src/locales/en/name/middle_name.ts diff --git a/src/locales/en/name/female_middle_name.ts b/src/locales/en/name/female_middle_name.ts new file mode 100644 index 00000000000..02a892fdccf --- /dev/null +++ b/src/locales/en/name/female_middle_name.ts @@ -0,0 +1,212 @@ +export default [ + 'Abigail', + 'Adele', + 'Alex', + 'Alice', + 'Alisha', + 'Amber', + 'Amelia', + 'Amora', + 'Anaïs', + 'Angelou', + 'Anika', + 'Anise', + 'Annabel', + 'Anne', + 'Aphrodite', + 'Aretha', + 'Arya', + 'Ashton', + 'Aster', + 'Audrey', + 'Avery', + 'Bailee', + 'Bay', + 'Belle', + 'Beth', + 'Billie', + 'Blair', + 'Blaise', + 'Blake', + 'Blanche', + 'Blue', + 'Bree', + 'Brielle', + 'Brienne', + 'Brooke', + 'Caleen', + 'Candice', + 'Caprice', + 'Carelyn', + 'Caylen', + 'Celine', + 'Cerise', + 'Cia', + 'Claire', + 'Claudia', + 'Clementine', + 'Coral', + 'Coraline', + 'Dahlia', + 'Dakota', + 'Dawn', + 'Della', + 'Demi', + 'Denise', + 'Denver', + 'Devine', + 'Devon', + 'Diana', + 'Dylan', + 'Ebony', + 'Eden', + 'Eleanor', + 'Elein', + 'Elizabeth', + 'Ellen', + 'Elodie', + 'Eloise', + 'Ember', + 'Emma', + 'Erin', + 'Eyre', + 'Faith', + 'Farrah', + 'Fawn', + 'Fayre', + 'Fern', + 'France', + 'Francis', + 'Frida', + 'Genisis', + 'Georgia', + 'Grace', + 'Gwen', + 'Harley', + 'Harper', + 'Hazel', + 'Helen', + 'Hippolyta', + 'Holly', + 'Hope', + 'Imani', + 'Iowa', + 'Ireland', + 'Irene', + 'Iris', + 'Isa', + 'Isla', + 'Ivy', + 'Jade', + 'Jane', + 'Jazz', + 'Jean', + 'Jess', + 'Jett', + 'Jo', + 'Joan', + 'Jolie', + 'Jordan', + 'Josie', + 'Journey', + 'Joy', + 'Jules', + 'Julien', + 'Juliet', + 'Juniper', + 'Justice', + 'Kali', + 'Karma', + 'Kat', + 'Kate', + 'Kennedy', + 'Keva', + 'Kylie', + 'Lake', + 'Lane', + 'Lark', + 'Layla', + 'Lee', + 'Leigh', + 'Leona', + 'Lexi', + 'London', + 'Lou', + 'Louise', + 'Love', + 'Luna', + 'Lux', + 'Lynn', + 'Lyric', + 'Maddie', + 'Mae', + 'Marie', + 'Matilda', + 'Maude', + 'Maybel', + 'Meadow', + 'Medusa', + 'Mercy', + 'Michelle', + 'Mirabel', + 'Monroe', + 'Morgan', + 'Nalia', + 'Naomi', + 'Nova', + 'Olive', + 'Paige', + 'Parker', + 'Pax', + 'Pearl', + 'Penelope', + 'Phoenix', + 'Quinn', + 'Rae', + 'Rain', + 'Raven', + 'Ray', + 'Raye', + 'Rebel', + 'Reese', + 'Reeve', + 'Regan', + 'Riley', + 'River', + 'Robin', + 'Rory', + 'Rose', + 'Royal', + 'Ruth', + 'Rylie', + 'Sage', + 'Sam', + 'Saturn', + 'Scout', + 'Serena', + 'Sky', + 'Skylar', + 'Sofia', + 'Sophia', + 'Storm', + 'Sue', + 'Suzanne', + 'Sydney', + 'Taylen', + 'Taylor', + 'Teagan', + 'Tempest', + 'Tenley', + 'Thea', + 'Trinity', + 'Valerie', + 'Venus', + 'Vera', + 'Violet', + 'Willow', + 'Winter', + 'Xena', + 'Zaylee', + 'Zion', + 'Zoe', +]; diff --git a/src/locales/en/name/index.ts b/src/locales/en/name/index.ts index 39f195db94d..dcc3fef143a 100644 --- a/src/locales/en/name/index.ts +++ b/src/locales/en/name/index.ts @@ -5,10 +5,13 @@ import type { NameDefinitions } from '../../..'; import binary_gender from './binary_gender'; import female_first_name from './female_first_name'; +import female_middle_name from './female_middle_name'; import first_name from './first_name'; import gender from './gender'; import last_name from './last_name'; import male_first_name from './male_first_name'; +import male_middle_name from './male_middle_name'; +import middle_name from './middle_name'; import name_ from './name'; import prefix from './prefix'; import suffix from './suffix'; @@ -17,10 +20,13 @@ import title from './title'; const name: Partial = { binary_gender, female_first_name, + female_middle_name, first_name, gender, last_name, male_first_name, + male_middle_name, + middle_name, name: name_, prefix, suffix, diff --git a/src/locales/en/name/male_middle_name.ts b/src/locales/en/name/male_middle_name.ts new file mode 100644 index 00000000000..6bd3ac848ba --- /dev/null +++ b/src/locales/en/name/male_middle_name.ts @@ -0,0 +1,100 @@ +export default [ + 'Ace', + 'Aiden', + 'Alexander', + 'Ander', + 'Anthony', + 'Asher', + 'August', + 'Aziel', + 'Bear', + 'Beckham', + 'Benjamin', + 'Buddy', + 'Calvin', + 'Carter', + 'Charles', + 'Christopher', + 'Clyde', + 'Cooper', + 'Daniel', + 'David', + 'Dior', + 'Dylan', + 'Elijah', + 'Ellis', + 'Emerson', + 'Ethan', + 'Ezra', + 'Fletcher', + 'Flynn', + 'Gabriel', + 'Grayson', + 'Gus', + 'Hank', + 'Harrison', + 'Hendrix', + 'Henry', + 'Houston', + 'Hudson', + 'Hugh', + 'Isaac', + 'Jack', + 'Jackson', + 'Jacob', + 'Jakobe', + 'James', + 'Jaxon', + 'Jaxtyn', + 'Jayden', + 'John', + 'Joseph', + 'Josiah', + 'Jude', + 'Julian', + 'Karsyn', + 'Kenji', + 'Kobe', + 'Kylo', + 'Lennon', + 'Leo', + 'Levi', + 'Liam', + 'Lincoln', + 'Logan', + 'Louis', + 'Lucas', + 'Lucky', + 'Luke', + 'Mason', + 'Mateo', + 'Matthew', + 'Maverick', + 'Michael', + 'Monroe', + 'Nixon', + 'Ocean', + 'Oliver', + 'Otis', + 'Otto', + 'Owen', + 'Ozzy', + 'Parker', + 'Rocky', + 'Samuel', + 'Sebastian', + 'Sonny', + 'Teddy', + 'Theo', + 'Theodore', + 'Thomas', + 'Truett', + 'Walter', + 'Warren', + 'Watson', + 'William', + 'Wison', + 'Wyatt', + 'Ziggy', + 'Zyair', +]; diff --git a/src/locales/en/name/middle_name.ts b/src/locales/en/name/middle_name.ts new file mode 100644 index 00000000000..0f03c1535dc --- /dev/null +++ b/src/locales/en/name/middle_name.ts @@ -0,0 +1,64 @@ +export default [ + 'Addison', + 'Alex', + 'Anderson', + 'Angel', + 'Arden', + 'August', + 'Austin', + 'Avery', + 'Bailey', + 'Billie', + 'Blake', + 'Bowie', + 'Brooklyn', + 'Cameron', + 'Charlie', + 'Corey', + 'Dakota', + 'Drew', + 'Elliott', + 'Ellis', + 'Emerson', + 'Finley', + 'Gray', + 'Greer', + 'Harper', + 'Hayden', + 'Jaden', + 'James', + 'Jamie', + 'Jordan', + 'Jules', + 'Kai', + 'Kendall', + 'Kennedy', + 'Kyle', + 'Leslie', + 'Logan', + 'London', + 'Marlowe', + 'Micah', + 'Nico', + 'Noah', + 'North', + 'Parker', + 'Phoenix', + 'Quinn', + 'Reagan', + 'Reese', + 'Reign', + 'Riley', + 'River', + 'Robin', + 'Rory', + 'Rowan', + 'Ryan', + 'Sage', + 'Sasha', + 'Sawyer', + 'Shawn', + 'Shiloh', + 'Skyler', + 'Taylor', +]; diff --git a/src/name.ts b/src/name.ts index 6b7275692eb..4a242449a47 100644 --- a/src/name.ts +++ b/src/name.ts @@ -126,8 +126,23 @@ export class Name { typeof this.faker.definitions.name.male_middle_name !== 'undefined' && typeof this.faker.definitions.name.female_middle_name !== 'undefined' ) { + if (typeof gender === 'string') { + if (gender.toLowerCase() === 'male') { + gender = 0; + } else if (gender.toLowerCase() === 'female') { + gender = 1; + } + } + if (typeof gender !== 'number') { - gender = this.faker.datatype.number(1); + if (typeof this.faker.definitions.name.middle_name === 'undefined') { + gender = this.faker.datatype.number(1); + } else { + // Fall back to unisex middle names if they exist and gender wasn't specified + return this.faker.random.arrayElement( + this.faker.definitions.name.middle_name + ); + } } if (gender === 0) { return this.faker.random.arrayElement( @@ -139,7 +154,6 @@ export class Name { ); } } - return this.faker.random.arrayElement( this.faker.definitions.name.middle_name ); diff --git a/test/name.spec.ts b/test/name.spec.ts index a8c0c986032..2f466e2c270 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -12,7 +12,7 @@ const seededRuns = [ noArgs: 'Hintz', }, middleName: { - noArgs: 'b', + noArgs: 'Greer', }, findName: { noArgs: 'Lorene Deckow', @@ -53,7 +53,7 @@ const seededRuns = [ noArgs: 'Gibson', }, middleName: { - noArgs: 'a', + noArgs: 'Dakota', }, findName: { noArgs: 'Marilyn Effertz', @@ -94,7 +94,7 @@ const seededRuns = [ noArgs: 'Ward', }, middleName: { - noArgs: 'c', + noArgs: 'Sawyer', }, findName: { noArgs: 'Darrel Sanford', @@ -247,6 +247,17 @@ describe('name', () => { expect(middle_name.length).greaterThan(0); }); + it('should return a middle name when passed en locale', () => { + faker.locale = 'en'; + + let name = faker.name.middleName(); + expect(faker.definitions.name.middle_name).toContain(name); + name = faker.name.middleName(0); + expect(faker.definitions.name.male_middle_name).toContain(name); + name = faker.name.middleName(1); + expect(faker.definitions.name.female_middle_name).toContain(name); + }); + it('should return a gender-specific middle name when passed a number', () => { faker.locale = 'uk'; From 58e816848b5d15e3d8caedb3a3885a96e51c8517 Mon Sep 17 00:00:00 2001 From: ganesh Date: Sun, 6 Feb 2022 13:23:23 -0500 Subject: [PATCH 2/3] refactor: added helper method & enabled middleName testcase for 'uk' locale --- src/name.ts | 40 ++++++++++++++++++++++++---------------- test/name.spec.ts | 18 +++++++----------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/name.ts b/src/name.ts index 4a242449a47..a7269b54c91 100644 --- a/src/name.ts +++ b/src/name.ts @@ -35,14 +35,7 @@ export class Name { // some locale datasets ( like ru ) have first_name split by gender. since the name.first_name field does not exist in these datasets, // we must randomly pick a name from either gender array so faker.name.firstName will return the correct locale data ( and not fallback ) - if (typeof gender === 'string') { - if (gender.toLowerCase() === 'male') { - gender = 0; - } else if (gender.toLowerCase() === 'female') { - gender = 1; - } - } - + gender = Name.determineGenderByStringParam(gender); if (typeof gender !== 'number') { if (typeof this.faker.definitions.name.first_name === 'undefined') { gender = this.faker.datatype.number(1); @@ -126,14 +119,7 @@ export class Name { typeof this.faker.definitions.name.male_middle_name !== 'undefined' && typeof this.faker.definitions.name.female_middle_name !== 'undefined' ) { - if (typeof gender === 'string') { - if (gender.toLowerCase() === 'male') { - gender = 0; - } else if (gender.toLowerCase() === 'female') { - gender = 1; - } - } - + gender = Name.determineGenderByStringParam(gender); if (typeof gender !== 'number') { if (typeof this.faker.definitions.name.middle_name === 'undefined') { gender = this.faker.datatype.number(1); @@ -346,4 +332,26 @@ export class Name { this.faker.definitions.name.title.job ); } + + /** + * + * Determines gender in 0 or 1 + * + * @param gender The optional gender to use. + * + * @returns 0 or 1 given a string param of 'male' or 'female' else returns default value + * + * @example Name.determineGenderByStringParam('male') // 0 + * + */ + private static determineGenderByStringParam(gender: string | number) { + if (typeof gender === 'string') { + if (gender.toLowerCase() === 'male') { + gender = 0; + } else if (gender.toLowerCase() === 'female') { + gender = 1; + } + } + return gender; + } } diff --git a/test/name.spec.ts b/test/name.spec.ts index 2f466e2c270..901aad2d0d7 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -267,18 +267,14 @@ describe('name', () => { expect(faker.definitions.name.female_middle_name).toContain(name); }); - // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 - it.todo( - 'should return a gender-specific middle name when passed a string', - () => { - faker.locale = 'uk'; + it('should return a gender-specific middle name when passed a string', () => { + faker.locale = 'uk'; - let name = faker.name.middleName('male'); - expect(faker.definitions.name.male_middle_name).toContain(name); - name = faker.name.middleName('female'); - expect(faker.definitions.name.female_middle_name).toContain(name); - } - ); + let name = faker.name.middleName('male'); + expect(faker.definitions.name.male_middle_name).toContain(name); + name = faker.name.middleName('female'); + expect(faker.definitions.name.female_middle_name).toContain(name); + }); }); describe('findName()', () => { From 3ce098830af57eb8d88834220754d9920a3244fc Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 21 Mar 2022 15:24:39 +0100 Subject: [PATCH 3/3] chore(revert): name.ts changes --- src/name.ts | 42 ++++++++++-------------------------------- test/name.spec.ts | 41 ++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 53 deletions(-) diff --git a/src/name.ts b/src/name.ts index a7269b54c91..6b7275692eb 100644 --- a/src/name.ts +++ b/src/name.ts @@ -35,7 +35,14 @@ export class Name { // some locale datasets ( like ru ) have first_name split by gender. since the name.first_name field does not exist in these datasets, // we must randomly pick a name from either gender array so faker.name.firstName will return the correct locale data ( and not fallback ) - gender = Name.determineGenderByStringParam(gender); + if (typeof gender === 'string') { + if (gender.toLowerCase() === 'male') { + gender = 0; + } else if (gender.toLowerCase() === 'female') { + gender = 1; + } + } + if (typeof gender !== 'number') { if (typeof this.faker.definitions.name.first_name === 'undefined') { gender = this.faker.datatype.number(1); @@ -119,16 +126,8 @@ export class Name { typeof this.faker.definitions.name.male_middle_name !== 'undefined' && typeof this.faker.definitions.name.female_middle_name !== 'undefined' ) { - gender = Name.determineGenderByStringParam(gender); if (typeof gender !== 'number') { - if (typeof this.faker.definitions.name.middle_name === 'undefined') { - gender = this.faker.datatype.number(1); - } else { - // Fall back to unisex middle names if they exist and gender wasn't specified - return this.faker.random.arrayElement( - this.faker.definitions.name.middle_name - ); - } + gender = this.faker.datatype.number(1); } if (gender === 0) { return this.faker.random.arrayElement( @@ -140,6 +139,7 @@ export class Name { ); } } + return this.faker.random.arrayElement( this.faker.definitions.name.middle_name ); @@ -332,26 +332,4 @@ export class Name { this.faker.definitions.name.title.job ); } - - /** - * - * Determines gender in 0 or 1 - * - * @param gender The optional gender to use. - * - * @returns 0 or 1 given a string param of 'male' or 'female' else returns default value - * - * @example Name.determineGenderByStringParam('male') // 0 - * - */ - private static determineGenderByStringParam(gender: string | number) { - if (typeof gender === 'string') { - if (gender.toLowerCase() === 'male') { - gender = 0; - } else if (gender.toLowerCase() === 'female') { - gender = 1; - } - } - return gender; - } } diff --git a/test/name.spec.ts b/test/name.spec.ts index 901aad2d0d7..2b8e9f0a304 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -12,7 +12,7 @@ const seededRuns = [ noArgs: 'Hintz', }, middleName: { - noArgs: 'Greer', + noArgs: 'Owen', }, findName: { noArgs: 'Lorene Deckow', @@ -53,7 +53,7 @@ const seededRuns = [ noArgs: 'Gibson', }, middleName: { - noArgs: 'Dakota', + noArgs: 'Kenji', }, findName: { noArgs: 'Marilyn Effertz', @@ -94,7 +94,7 @@ const seededRuns = [ noArgs: 'Ward', }, middleName: { - noArgs: 'Sawyer', + noArgs: 'Isla', }, findName: { noArgs: 'Darrel Sanford', @@ -188,16 +188,12 @@ describe('name', () => { expect(faker.definitions.name.female_first_name).toContain(name); }); - // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 - it.todo( - 'should return a gender-specific first name when passed a string', - () => { - let name = faker.name.firstName('male'); - expect(faker.definitions.name.male_first_name).toContain(name); - name = faker.name.firstName('female'); - expect(faker.definitions.name.female_first_name).toContain(name); - } - ); + it('should return a gender-specific first name when passed a string', () => { + let name = faker.name.firstName('male'); + expect(faker.definitions.name.male_first_name).toContain(name); + name = faker.name.firstName('female'); + expect(faker.definitions.name.female_first_name).toContain(name); + }); }); describe('lastName()', () => { @@ -247,7 +243,7 @@ describe('name', () => { expect(middle_name.length).greaterThan(0); }); - it('should return a middle name when passed en locale', () => { + it.todo('should return a middle name when passed en locale', () => { faker.locale = 'en'; let name = faker.name.middleName(); @@ -267,14 +263,17 @@ describe('name', () => { expect(faker.definitions.name.female_middle_name).toContain(name); }); - it('should return a gender-specific middle name when passed a string', () => { - faker.locale = 'uk'; + it.todo( + 'should return a gender-specific middle name when passed a string', + () => { + faker.locale = 'uk'; - let name = faker.name.middleName('male'); - expect(faker.definitions.name.male_middle_name).toContain(name); - name = faker.name.middleName('female'); - expect(faker.definitions.name.female_middle_name).toContain(name); - }); + let name = faker.name.middleName('male'); + expect(faker.definitions.name.male_middle_name).toContain(name); + name = faker.name.middleName('female'); + expect(faker.definitions.name.female_middle_name).toContain(name); + } + ); }); describe('findName()', () => {