From 62e92a8241c4906ec0b10c6466148ee050d9f609 Mon Sep 17 00:00:00 2001 From: kabrunko-dev Date: Tue, 26 Dec 2023 09:32:42 -0300 Subject: [PATCH 1/2] =?UTF-8?q?feat(i18n-ptBR):=20=F0=9F=94=A5=20add=20fir?= =?UTF-8?q?st,=20last=20and=20full=20name=20randomizer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../falso/src/lib/i18n/pt-br/first-name.ts | 36 +++++++++++++ .../falso/src/lib/i18n/pt-br/full-name.ts | 50 +++++++++++++++++++ .../falso/src/lib/i18n/pt-br/last-name.ts | 32 ++++++++++++ packages/falso/src/tests/first-name.spec.ts | 16 +++--- packages/falso/src/tests/full-name.spec.ts | 47 +++++++++++++++++ packages/falso/src/tests/last-name.spec.ts | 20 ++------ 6 files changed, 177 insertions(+), 24 deletions(-) create mode 100644 packages/falso/src/lib/i18n/pt-br/first-name.ts create mode 100644 packages/falso/src/lib/i18n/pt-br/full-name.ts create mode 100644 packages/falso/src/lib/i18n/pt-br/last-name.ts diff --git a/packages/falso/src/lib/i18n/pt-br/first-name.ts b/packages/falso/src/lib/i18n/pt-br/first-name.ts new file mode 100644 index 000000000..734c4dd82 --- /dev/null +++ b/packages/falso/src/lib/i18n/pt-br/first-name.ts @@ -0,0 +1,36 @@ +import { NameOptions } from '../../full-name'; +import { randFirstName as randFirstNameDefault } from '../../first-name'; +import { data as locale } from './first-name.i18n.json'; + +/** + * Generate a random first name. + * + * @category person + * + * @example + * + * randFirstName() + * + * @example + * + * randFirstName({ withAccents: true }) + * + * @example + * + * randFirstName({ gender: 'female' }) // Emma + * + * @example + * + * randFirstName({ length: 10 }) + * + */ +export function randFirstName( + options?: Options +) { + const _options = { + ...options, + locale, + }; + + return randFirstNameDefault(_options); +} diff --git a/packages/falso/src/lib/i18n/pt-br/full-name.ts b/packages/falso/src/lib/i18n/pt-br/full-name.ts new file mode 100644 index 000000000..a6966f290 --- /dev/null +++ b/packages/falso/src/lib/i18n/pt-br/full-name.ts @@ -0,0 +1,50 @@ +import { fake } from '../../core/core'; +import { randFirstName } from '../../first-name'; +import { NameOptions } from '../../full-name'; +import { randLastName } from '../../last-name'; + +import { data as localeFirstName } from './first-name.i18n.json'; +import { data as localeLastName } from './last-name.i18n.json'; + +/** + * Generate a random full name. + * + * @category person + * + * @example + * + * randFullName() + * + * @example + * + * randFullName({ gender: 'female' }) // Emma Marková + * + * @example + * + * randFullName({ withAccents: false }) // return full name without special symbols like â, î or ô and etc + * + * @example + * + * randFullName({ length: 10 }) + * + */ +export function randFullName( + options?: Options +) { + const nameOptions = { + withAccents: options?.withAccents, + gender: options?.gender, + }; + + const firstName = randFirstName({ + ...nameOptions, + locale: localeFirstName, + }); + + const lastName = randLastName({ + ...nameOptions, + locale: localeLastName, + }); + + return fake(() => `${firstName} ${lastName}`, options); +} diff --git a/packages/falso/src/lib/i18n/pt-br/last-name.ts b/packages/falso/src/lib/i18n/pt-br/last-name.ts new file mode 100644 index 000000000..7f38a92a5 --- /dev/null +++ b/packages/falso/src/lib/i18n/pt-br/last-name.ts @@ -0,0 +1,32 @@ +import { NameOptions } from '../../full-name'; +import { randLastName as randLastNameDefault } from '../../last-name'; +import { data as locale } from './last-name.i18n.json'; + +/** + * Generate a random last name. + * + * @category person + * + * @example + * + * randLastName() + * + * @example + * + * randLastName({ withAccents: false }) + * + * @example + * + * randLastName({ length: 10 }) + * + */ +export function randLastName( + options?: Options +) { + const _options = { + ...options, + locale, + }; + + return randLastNameDefault(_options); +} diff --git a/packages/falso/src/tests/first-name.spec.ts b/packages/falso/src/tests/first-name.spec.ts index 334d19160..60bcb7dcd 100644 --- a/packages/falso/src/tests/first-name.spec.ts +++ b/packages/falso/src/tests/first-name.spec.ts @@ -1,9 +1,11 @@ import { randFirstName } from '../lib/first-name'; import { data } from '../lib/first-name.json'; +import { NameOptions } from '../lib/full-name'; -import { data as locale_ru } from '../lib/i18n/ru/first-name.i18n.json'; +import { randFirstName as randFirstNamePtBR } from '../lib/i18n/pt-br/first-name'; import { data as locale_ptBR } from '../lib/i18n/pt-br/first-name.i18n.json'; -import { NameOptions } from '../lib/full-name'; + +import { data as locale_ru } from '../lib/i18n/ru/first-name.i18n.json'; describe('firstName', () => { let specialCharRegex: RegExp; @@ -170,12 +172,11 @@ describe('firstName', () => { beforeEach(() => { options = { gender: 'female', - locale: data, }; }); it('should return a firstName with at least 1 accented character', () => { - const result = randFirstName({ + const result = randFirstNamePtBR({ ...options, withAccents: true, }); @@ -185,7 +186,7 @@ describe('firstName', () => { }); it('should return a firstName with only non-accented characters', () => { - const result = randFirstName({ + const result = randFirstNamePtBR({ ...options, withAccents: false, }); @@ -199,12 +200,11 @@ describe('firstName', () => { beforeEach(() => { options = { gender: 'male', - locale: data, }; }); it('should return a firstName with at least 1 accented character', () => { - const result = randFirstName({ + const result = randFirstNamePtBR({ ...options, withAccents: true, }); @@ -214,7 +214,7 @@ describe('firstName', () => { }); it('should return a firstName with only non-accented characters', () => { - const result = randFirstName({ + const result = randFirstNamePtBR({ ...options, withAccents: false, }); diff --git a/packages/falso/src/tests/full-name.spec.ts b/packages/falso/src/tests/full-name.spec.ts index 3ae764ca8..8fde9a6f1 100644 --- a/packages/falso/src/tests/full-name.spec.ts +++ b/packages/falso/src/tests/full-name.spec.ts @@ -1,5 +1,7 @@ import { randFullName } from '../lib/full-name'; +import { randFullName as randFullNamePtBr } from '../lib/i18n/pt-br/full-name'; + describe('fullName', () => { let specialCharRegex: RegExp; let nameStructureRegex: RegExp | string; @@ -62,4 +64,49 @@ describe('fullName', () => { expect(result3).not.toContain(','); }); }); + + describe('locale PT-BR', () => { + describe('withAccents is passed', () => { + let result: string; + + describe('value is true', () => { + beforeEach(() => { + result = randFullNamePtBr({ withAccents: true }); + }); + + it('should return a string with a first name, a space and then last name', () => { + expect(result).toMatch(nameStructureRegex); + }); + + it('should return a string containing accents', () => { + expect(result).toMatch(specialCharRegex); + }); + }); + + describe('value is false', () => { + beforeEach(() => { + result = randFullNamePtBr({ withAccents: false }); + }); + + it('should return a string with a first name, a space and then last name', () => { + expect(result).toMatch(nameStructureRegex); + }); + + it('should not return a string containing accents', () => { + expect(result).not.toMatch(specialCharRegex); + }); + }); + }); + + describe('length is passed', () => { + it('should not contain commas', () => { + // Bug fix #100: Length option was passed to randFirstName & randLastName + const [result1, result2, result3] = randFullNamePtBr({ length: 3 }); + + expect(result1).not.toContain(','); + expect(result2).not.toContain(','); + expect(result3).not.toContain(','); + }); + }); + }); }); diff --git a/packages/falso/src/tests/last-name.spec.ts b/packages/falso/src/tests/last-name.spec.ts index 4abcb07d9..352ad7d79 100644 --- a/packages/falso/src/tests/last-name.spec.ts +++ b/packages/falso/src/tests/last-name.spec.ts @@ -1,8 +1,9 @@ import { randLastName } from '../lib/last-name'; -import { NameOptions } from '../lib/full-name'; import * as randBooleanFunctions from '../lib/boolean'; import { data } from '../lib/last-name.json'; + +import { randLastName as randLastNamePtBr } from '../lib/i18n/pt-br/last-name'; import { data as locale_ptBR } from '../lib/i18n/pt-br/last-name.i18n.json'; describe('lastName', () => { @@ -114,29 +115,16 @@ describe('lastName', () => { describe('with provided locale PT-BR data', () => { const data = locale_ptBR; - let options: NameOptions; - - beforeEach(() => { - options = { - locale: data, - }; - }); it('should return a lastName with at least 1 accented character', () => { - const result = randLastName({ - ...options, - withAccents: true, - }); + const result = randLastNamePtBr({ withAccents: true }); expect(result.match(specialCharRegex)).toBeTruthy(); expect(data.withAccents.includes(result)).toBe(true); }); it('should return a lastName with only non-accented characters', () => { - const result = randLastName({ - ...options, - withAccents: false, - }); + const result = randLastNamePtBr({ withAccents: false }); expect(result.match(specialCharRegex)).toBeFalsy(); expect(data.withoutAccents.includes(result)).toBe(true); From ff5e00e24ef8c72cf7371a8fcf69bf2469f3a949 Mon Sep 17 00:00:00 2001 From: kabrunko-dev Date: Tue, 26 Dec 2023 13:56:16 -0300 Subject: [PATCH 2/2] =?UTF-8?q?refactor(i18n-ptBR):=20=F0=9F=92=A1=20add?= =?UTF-8?q?=20fullname=20randomizer=20arg=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/falso/src/lib/i18n/pt-br/full-name.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/falso/src/lib/i18n/pt-br/full-name.ts b/packages/falso/src/lib/i18n/pt-br/full-name.ts index a6966f290..8d05064c4 100644 --- a/packages/falso/src/lib/i18n/pt-br/full-name.ts +++ b/packages/falso/src/lib/i18n/pt-br/full-name.ts @@ -1,11 +1,15 @@ -import { fake } from '../../core/core'; +import { FakeOptions, fake } from '../../core/core'; import { randFirstName } from '../../first-name'; -import { NameOptions } from '../../full-name'; import { randLastName } from '../../last-name'; import { data as localeFirstName } from './first-name.i18n.json'; import { data as localeLastName } from './last-name.i18n.json'; +export interface NameOptions extends FakeOptions { + withAccents?: boolean; + gender?: 'male' | 'female'; +} + /** * Generate a random full name. *