-
-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: special characters in emails (#792)
- Loading branch information
1 parent
4ac2a04
commit 3b5a21f
Showing
2 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters