Skip to content

Commit 3b5a21f

Browse files
authored
feat: special characters in emails (#792)
1 parent 4ac2a04 commit 3b5a21f

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/internet.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,64 @@ export class Internet {
3434
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
3535
* @param lastName The optional last name to use. If not specified, a random one will be chosen.
3636
* @param provider The mail provider domain to use. If not specified, a random free mail provider will be chosen.
37+
* @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`.
38+
* @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included
39+
* in the email address. Defaults to `false`.
3740
*
3841
* @example
3942
* faker.internet.email() // '[email protected]'
4043
* faker.internet.email('Jeanne', 'Doe') // '[email protected]'
4144
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev') // '[email protected]'
45+
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%[email protected]'
4246
*/
43-
email(firstName?: string, lastName?: string, provider?: string): string {
47+
email(
48+
firstName?: string,
49+
lastName?: string,
50+
provider?: string,
51+
options?: { allowSpecialCharacters?: boolean }
52+
): string {
4453
provider =
4554
provider ||
4655
this.faker.random.arrayElement(
4756
this.faker.definitions.internet.free_email
4857
);
49-
return (
50-
this.faker.helpers.slugify(
51-
this.faker.internet.userName(firstName, lastName)
52-
) +
53-
'@' +
54-
provider
58+
let localPart: string = this.faker.helpers.slugify(
59+
this.faker.internet.userName(firstName, lastName)
5560
);
61+
if (options?.allowSpecialCharacters) {
62+
const usernameChars: string[] = '._-'.split('');
63+
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
64+
localPart = localPart.replace(
65+
this.faker.random.arrayElement(usernameChars),
66+
this.faker.random.arrayElement(specialChars)
67+
);
68+
}
69+
return `${localPart}@${provider}`;
5670
}
5771

5872
/**
5973
* Generates an email address using an example mail provider using the given person's name as base.
6074
*
6175
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
6276
* @param lastName The optional last name to use. If not specified, a random one will be chosen.
77+
* @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`.
78+
* @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included
79+
* in the email address. Defaults to `false`.
6380
*
6481
* @example
6582
* faker.internet.exampleEmail() // '[email protected]'
6683
* faker.internet.exampleEmail('Jeanne', 'Doe') // '[email protected]'
84+
* faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%[email protected]'
6785
*/
68-
exampleEmail(firstName?: string, lastName?: string): string {
86+
exampleEmail(
87+
firstName?: string,
88+
lastName?: string,
89+
options?: { allowSpecialCharacters?: boolean }
90+
): string {
6991
const provider = this.faker.random.arrayElement(
7092
this.faker.definitions.internet.example_email
7193
);
72-
return this.email(firstName, lastName, provider);
94+
return this.email(firstName, lastName, provider, options);
7395
}
7496

7597
/**

test/internet.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ describe('internet', () => {
191191
expect(prefix).match(/^_3/);
192192
expect(faker.definitions.internet.free_email).toContain(suffix);
193193
});
194+
195+
it('should return an email with special characters', () => {
196+
const email = faker.internet.email('Mike', 'Smith', null, {
197+
allowSpecialCharacters: true,
198+
});
199+
200+
expect(email).toBeTruthy();
201+
expect(email).toBeTypeOf('string');
202+
expect(email).satisfy(validator.isEmail);
203+
204+
const [prefix, suffix] = email.split('@');
205+
206+
expect(prefix).match(/^Mike([.!#$%&'*+-/=?^_`{|}~]Smith)?\d*/);
207+
expect(faker.definitions.internet.free_email).toContain(suffix);
208+
});
194209
});
195210

196211
describe('exampleEmail()', () => {
@@ -248,6 +263,22 @@ describe('internet', () => {
248263
expect(faker.definitions.internet.example_email).toContain(suffix);
249264
expect(prefix).match(/^_3/);
250265
});
266+
267+
it('should return an email with special characters', () => {
268+
const email = faker.internet.exampleEmail('Mike', 'Smith', {
269+
allowSpecialCharacters: true,
270+
});
271+
272+
expect(email).toBeTruthy();
273+
expect(email).toBeTypeOf('string');
274+
expect(email).satisfy(validator.isEmail);
275+
276+
const [prefix, suffix] = email.split('@');
277+
278+
expect(suffix).match(/^example\.(com|net|org)$/);
279+
expect(faker.definitions.internet.example_email).toContain(suffix);
280+
expect(prefix).match(/^Mike([.!#$%&'*+-/=?^_`{|}~]Smith)?\d*/);
281+
});
251282
});
252283

253284
describe('userName()', () => {

0 commit comments

Comments
 (0)