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: special characters in emails #792

Merged
merged 13 commits into from
Apr 8, 2022
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
40 changes: 31 additions & 9 deletions src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,64 @@ 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() // '[email protected]'
* faker.internet.email('Jeanne', 'Doe') // '[email protected]'
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev') // '[email protected]'
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%[email protected]'
*/
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}`;
}

/**
* Generates an email address using an example mail provider using the given person's name as base.
*
* @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() // '[email protected]'
* faker.internet.exampleEmail('Jeanne', 'Doe') // '[email protected]'
* faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%[email protected]'
*/
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);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ describe('internet', () => {
expect(prefix).match(/^思源_唐3/);
expect(faker.definitions.internet.free_email).toContain(suffix);
});

it('should return an email with special characters', () => {
const email = faker.internet.email('Mike', 'Smith', null, {
allowSpecialCharacters: true,
});

expect(email).toBeTruthy();
expect(email).toBeTypeOf('string');
expect(email).satisfy(validator.isEmail);

const [prefix, suffix] = email.split('@');

expect(prefix).match(/^Mike([.!#$%&'*+-/=?^_`{|}~]Smith)?\d*/);
expect(faker.definitions.internet.free_email).toContain(suffix);
});
});

describe('exampleEmail()', () => {
Expand Down Expand Up @@ -248,6 +263,22 @@ describe('internet', () => {
expect(faker.definitions.internet.example_email).toContain(suffix);
expect(prefix).match(/^思源_唐3/);
});

it('should return an email with special characters', () => {
const email = faker.internet.exampleEmail('Mike', 'Smith', {
allowSpecialCharacters: true,
});

expect(email).toBeTruthy();
expect(email).toBeTypeOf('string');
expect(email).satisfy(validator.isEmail);

const [prefix, suffix] = email.split('@');

expect(suffix).match(/^example\.(com|net|org)$/);
expect(faker.definitions.internet.example_email).toContain(suffix);
expect(prefix).match(/^Mike([.!#$%&'*+-/=?^_`{|}~]Smith)?\d*/);
});
});

describe('userName()', () => {
Expand Down