Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n-ptBR): 🔥 add first, last and full name randomizer #375

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
54 changes: 54 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,54 @@
import { FakeOptions, fake } from '../../core/core';
import { randFirstName } from '../../first-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.
*
* @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
Loading