From 3b5a21f3aae52f263f2c91e763fcee613092166c Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Fri, 8 Apr 2022 10:34:05 -0600 Subject: feat: special characters in emails (#792) --- src/internet.ts | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/internet.ts b/src/internet.ts index 7615607f..08ac2e91 100644 --- a/src/internet.ts +++ b/src/internet.ts @@ -34,25 +34,39 @@ export class Internet { * @param firstName The optional first name to use. If not specified, a random one will be chosen. * @param lastName The optional last name to use. If not specified, a random one will be chosen. * @param provider The mail provider domain to use. If not specified, a random free mail provider will be chosen. + * @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`. + * @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included + * in the email address. Defaults to `false`. * * @example * faker.internet.email() // 'Kassandra4@hotmail.com' * faker.internet.email('Jeanne', 'Doe') // 'Jeanne63@yahoo.com' * faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev') // 'Jeanne_Doe88@example.fakerjs.dev' + * faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.fakerjs.dev' */ - email(firstName?: string, lastName?: string, provider?: string): string { + email( + firstName?: string, + lastName?: string, + provider?: string, + options?: { allowSpecialCharacters?: boolean } + ): string { provider = provider || this.faker.random.arrayElement( this.faker.definitions.internet.free_email ); - return ( - this.faker.helpers.slugify( - this.faker.internet.userName(firstName, lastName) - ) + - '@' + - provider + let localPart: string = this.faker.helpers.slugify( + this.faker.internet.userName(firstName, lastName) ); + if (options?.allowSpecialCharacters) { + const usernameChars: string[] = '._-'.split(''); + const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split(''); + localPart = localPart.replace( + this.faker.random.arrayElement(usernameChars), + this.faker.random.arrayElement(specialChars) + ); + } + return `${localPart}@${provider}`; } /** @@ -60,16 +74,24 @@ export class Internet { * * @param firstName The optional first name to use. If not specified, a random one will be chosen. * @param lastName The optional last name to use. If not specified, a random one will be chosen. + * @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`. + * @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included + * in the email address. Defaults to `false`. * * @example * faker.internet.exampleEmail() // 'Helmer.Graham23@example.com' * faker.internet.exampleEmail('Jeanne', 'Doe') // 'Jeanne96@example.net' + * faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.com' */ - exampleEmail(firstName?: string, lastName?: string): string { + exampleEmail( + firstName?: string, + lastName?: string, + options?: { allowSpecialCharacters?: boolean } + ): string { const provider = this.faker.random.arrayElement( this.faker.definitions.internet.example_email ); - return this.email(firstName, lastName, provider); + return this.email(firstName, lastName, provider, options); } /** -- cgit v1.2.3