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

add withAccents option to randUserName() #356

Merged
merged 2 commits into from
Jun 30, 2023
Merged
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
13 changes: 11 additions & 2 deletions packages/falso/src/lib/user-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { randLastName } from './last-name';
export interface UserNameOptions extends FakeOptions {
firstName?: string;
lastName?: string;
withAccents?: boolean;
}

/**
Expand All @@ -30,13 +31,21 @@ export interface UserNameOptions extends FakeOptions {
*
* randUserName({ lastName: 'Smee' })
*
* @example
*
* randUserName({ withAccents: false }) // return username without special symbols like â, î or ô and etc
*
*/
export function randUserName<Options extends UserNameOptions = never>(
options?: Options
) {
const nameOptions = {
withAccents: options?.withAccents,
};

return fake(() => {
const firstName = options?.firstName ?? randFirstName();
const lastName = options?.lastName ?? randLastName();
const firstName = options?.firstName ?? randFirstName(nameOptions);
const lastName = options?.lastName ?? randLastName(nameOptions);
let userName = `${firstName} ${lastName}`.replace(' ', fake(['.', '_']));

if (randBoolean()) {
Expand Down
30 changes: 30 additions & 0 deletions packages/falso/src/tests/user-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ describe('username', () => {
expect(result).toContain(lastName);
});
});

describe('withAccents is passed', () => {
let withAccents: boolean;
const specialCharRegex =
/[āĀàÀáÁâÂãÃäÄÅåæÆçÇčČćĆðÐēĒèÈéÉêÊĚěëËėĖìÌíÍîÎïÏłŁñÑńŃōŌøØòÒóÓôÔõÕöÖőŐœŒřŘšŠßÞþùÙúÚûÛūŪüÜýÝÿŸžŽżŻ]/;

describe('withAccents is true', () => {
beforeEach(() => {
withAccents = true;
});

it('should return a string containing accents', () => {
const result = randUserName({ withAccents });

expect(result).toMatch(specialCharRegex);
});
});

describe('withAccents is false', () => {
beforeEach(() => {
withAccents = false;
});

it('should not return a string containing accents', () => {
const result = randUserName({ withAccents });

expect(result).not.toMatch(specialCharRegex);
});
});
});
});