diff --git a/packages/falso/src/lib/user-name.ts b/packages/falso/src/lib/user-name.ts index e005a45f..1e144685 100644 --- a/packages/falso/src/lib/user-name.ts +++ b/packages/falso/src/lib/user-name.ts @@ -7,6 +7,7 @@ import { randLastName } from './last-name'; export interface UserNameOptions extends FakeOptions { firstName?: string; lastName?: string; + withAccents?: boolean; } /** @@ -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?: 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()) { diff --git a/packages/falso/src/tests/user-name.spec.ts b/packages/falso/src/tests/user-name.spec.ts index e848a5ff..2b3f9da8 100644 --- a/packages/falso/src/tests/user-name.spec.ts +++ b/packages/falso/src/tests/user-name.spec.ts @@ -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); + }); + }); + }); });