@@ -34,42 +34,64 @@ export class Internet {
34
34
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
35
35
* @param lastName The optional last name to use. If not specified, a random one will be chosen.
36
36
* @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`.
37
40
*
38
41
* @example
39
42
* faker.internet.email() // '[email protected] '
40
43
* faker.internet.email('Jeanne', 'Doe') // '[email protected] '
41
44
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev') // '[email protected] '
45
+ * faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%[email protected] '
42
46
*/
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 {
44
53
provider =
45
54
provider ||
46
55
this . faker . random . arrayElement (
47
56
this . faker . definitions . internet . free_email
48
57
) ;
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 )
55
60
) ;
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 } ` ;
56
70
}
57
71
58
72
/**
59
73
* Generates an email address using an example mail provider using the given person's name as base.
60
74
*
61
75
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
62
76
* @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`.
63
80
*
64
81
* @example
65
82
* faker.internet.exampleEmail() // '[email protected] '
66
83
* faker.internet.exampleEmail('Jeanne', 'Doe') // '[email protected] '
84
+ * faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%[email protected] '
67
85
*/
68
- exampleEmail ( firstName ?: string , lastName ?: string ) : string {
86
+ exampleEmail (
87
+ firstName ?: string ,
88
+ lastName ?: string ,
89
+ options ?: { allowSpecialCharacters ?: boolean }
90
+ ) : string {
69
91
const provider = this . faker . random . arrayElement (
70
92
this . faker . definitions . internet . example_email
71
93
) ;
72
- return this . email ( firstName , lastName , provider ) ;
94
+ return this . email ( firstName , lastName , provider , options ) ;
73
95
}
74
96
75
97
/**
0 commit comments