@@ -2,11 +2,27 @@ import type { Random } from '../random'
22import type { LocaleDefinition , PasswordOptions } from '../types'
33
44export class InternetModule {
5+ private static readonly SEPARATORS = [ '.' , '_' , '-' , '' ]
6+ private domainWordsCache : string [ ] | null = null
7+
58 constructor (
69 private random : Random ,
710 private locale : LocaleDefinition ,
811 ) { }
912
13+ /**
14+ * Get cached lowercase domain words
15+ */
16+ private getDomainWords ( ) : string [ ] {
17+ if ( ! this . domainWordsCache ) {
18+ const companyNames = this . locale . company . name ?? [ 'company' , 'tech' , 'digital' ]
19+ this . domainWordsCache = companyNames . map ( name =>
20+ name . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ,
21+ ) . filter ( name => name . length > 0 )
22+ }
23+ return this . domainWordsCache
24+ }
25+
1026 /**
1127 * Generate a random email address
1228 * @example faker.internet.email() // 'john.doe@example.com'
@@ -15,7 +31,7 @@ export class InternetModule {
1531 // Optimized: avoid array spreading by selecting from combined length
1632 let firstName : string
1733 if ( options ?. firstName ) {
18- firstName = options . firstName
34+ firstName = options . firstName . toLowerCase ( )
1935 }
2036 else {
2137 const maleLen = this . locale . person . firstNameMale . length
@@ -37,11 +53,10 @@ export class InternetModule {
3753
3854 const lastName = options ?. lastName ?? this . random . arrayElement ( this . locale . person . lastName ) . toLowerCase ( )
3955 const provider = options ?. provider ?? this . random . arrayElement ( this . locale . internet . domainSuffix ?? [ 'com' , 'net' , 'org' ] )
56+ const separator = this . random . arrayElement ( InternetModule . SEPARATORS )
57+ const domain = this . random . arrayElement ( this . getDomainWords ( ) )
4058
41- const separators = [ '.' , '_' , '-' , '' ]
42- const separator = this . random . arrayElement ( separators )
43-
44- return `${ firstName } ${ separator } ${ lastName } @${ this . domainWord ( ) } .${ provider } `
59+ return `${ firstName } ${ separator } ${ lastName } @${ domain } .${ provider } `
4560 }
4661
4762 /**
@@ -182,9 +197,7 @@ export class InternetModule {
182197 * @example faker.internet.domainWord() // 'example'
183198 */
184199 domainWord ( ) : string {
185- return this . random . arrayElement ( this . locale . company . name ?? [ 'Company' , 'Tech' , 'Digital' ] )
186- . toLowerCase ( )
187- . replace ( / [ ^ a - z 0 - 9 ] / g, '' )
200+ return this . random . arrayElement ( this . getDomainWords ( ) )
188201 }
189202
190203 /**
0 commit comments