Skip to content

Commit

Permalink
feat(i18n-ptBR): 🔥 add first, last and full name randomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
kabrunko-dev committed Dec 26, 2023
1 parent 68dbb27 commit 62e92a8
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 24 deletions.
36 changes: 36 additions & 0 deletions packages/falso/src/lib/i18n/pt-br/first-name.ts
Original file line number Diff line number Diff line change
@@ -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 extends NameOptions = never>(
options?: Options
) {
const _options = {
...options,
locale,
};

return randFirstNameDefault(_options);
}
50 changes: 50 additions & 0 deletions packages/falso/src/lib/i18n/pt-br/full-name.ts
Original file line number Diff line number Diff line change
@@ -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 extends NameOptions = never>(
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);
}
32 changes: 32 additions & 0 deletions packages/falso/src/lib/i18n/pt-br/last-name.ts
Original file line number Diff line number Diff line change
@@ -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 extends NameOptions = never>(
options?: Options
) {
const _options = {
...options,
locale,
};

return randLastNameDefault(_options);
}
16 changes: 8 additions & 8 deletions packages/falso/src/tests/first-name.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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,
});
Expand Down
47 changes: 47 additions & 0 deletions packages/falso/src/tests/full-name.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(',');
});
});
});
});
20 changes: 4 additions & 16 deletions packages/falso/src/tests/last-name.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 62e92a8

Please sign in to comment.