Skip to content

Commit 34cf364

Browse files
authored
refactor(internet): rename userName method to username (#3130)
1 parent e271d4a commit 34cf364

File tree

8 files changed

+212
-15
lines changed

8 files changed

+212
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const { faker } = require('@faker-js/faker');
6565
export function createRandomUser() {
6666
return {
6767
userId: faker.string.uuid(),
68-
username: faker.internet.userName(),
68+
username: faker.internet.username(), // before version 9.1.0, use userName()
6969
email: faker.internet.email(),
7070
avatar: faker.image.avatar(),
7171
password: faker.internet.password(),

docs/api/ApiIndex.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ const filtered = computed(() => {
6969
</h3>
7070
<ul>
7171
<li v-for="h of item.headers" :key="h.anchor">
72+
<!-- TODO @ST-DDT 2024-09-25: Remove this in v10 -->
7273
<a
73-
:href="item.link + '#' + slugify(h.anchor)"
74+
:href="
75+
item.link +
76+
'#' +
77+
(h.anchor === 'userName' ? 'username-1' : slugify(h.anchor))
78+
"
7479
:class="{ deprecated: h.deprecated }"
7580
>{{ h.text }}</a
7681
>

docs/guide/frameworks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import { faker } from '@faker-js/faker/locale/en';
7171

7272
describe('Testing the application', () => {
7373
it('should create an account with username and password', () => {
74-
let username = faker.internet.userName();
74+
let username = faker.internet.username(); // before version 9.1.0, use userName()
7575
let password = faker.internet.password();
7676
let email = faker.internet.exampleEmail();
7777

@@ -111,7 +111,7 @@ test.describe('Testing the application', () => {
111111
test('should create an account with username and password', async ({
112112
page,
113113
}) => {
114-
const username = faker.internet.userName();
114+
const username = faker.internet.username(); // before version 9.1.0, use userName()
115115
const password = faker.internet.password();
116116
const email = faker.internet.exampleEmail();
117117

src/modules/git/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class GitModule extends ModuleBase {
8888
const firstName = this.faker.person.firstName();
8989
const lastName = this.faker.person.lastName();
9090
const fullName = this.faker.person.fullName({ firstName, lastName });
91-
const username = this.faker.internet.userName({ firstName, lastName });
91+
const username = this.faker.internet.username({ firstName, lastName });
9292
let user = this.faker.helpers.arrayElement([fullName, username]);
9393
const email = this.faker.internet.email({ firstName, lastName });
9494

src/modules/internet/index.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FakerError } from '../../errors/faker-error';
2+
import { deprecated } from '../../internal/deprecated';
23
import { ModuleBase } from '../../internal/module-base';
34
import { charMapping } from './char-mappings';
45
import * as random_ua from './user-agent';
@@ -105,7 +106,7 @@ const ipv4Networks: Record<IPv4Network, string> = {
105106
*
106107
* ### Overview
107108
*
108-
* For user accounts, you may need an [`email()`](https://fakerjs.dev/api/internet.html#email) and a [`password()`](https://fakerjs.dev/api/internet.html#password), as well as a ASCII [`userName()`](https://fakerjs.dev/api/internet.html#username) or Unicode [`displayName()`](https://fakerjs.dev/api/internet.html#displayname). Since the emails generated could coincidentally be real email addresses, you should not use these for sending real email addresses. If this is a concern, use [`exampleEmail()`](https://fakerjs.dev/api/internet.html#exampleemail) instead.
109+
* For user accounts, you may need an [`email()`](https://fakerjs.dev/api/internet.html#email) and a [`password()`](https://fakerjs.dev/api/internet.html#password), as well as a ASCII [`username()`](https://fakerjs.dev/api/internet.html#username) or Unicode [`displayName()`](https://fakerjs.dev/api/internet.html#displayname). Since the emails generated could coincidentally be real email addresses, you should not use these for sending real email addresses. If this is a concern, use [`exampleEmail()`](https://fakerjs.dev/api/internet.html#exampleemail) instead.
109110
*
110111
* For websites, you can generate a [`domainName()`](https://fakerjs.dev/api/internet.html#domainname) or a full [`url()`](https://fakerjs.dev/api/internet.html#url).
111112
*
@@ -169,7 +170,7 @@ export class InternetModule extends ModuleBase {
169170
allowSpecialCharacters = false,
170171
} = options;
171172

172-
let localPart: string = this.userName({ firstName, lastName });
173+
let localPart: string = this.username({ firstName, lastName });
173174
// Strip any special characters from the local part of the email address
174175
// This could happen if invalid chars are passed in manually in the firstName/lastName
175176
localPart = localPart.replaceAll(/[^A-Za-z0-9._+-]+/g, '');
@@ -273,6 +274,8 @@ export class InternetModule extends ModuleBase {
273274
* faker.internet.userName({ firstName: '大羽', lastName: '陳' }) // 'hlzp8d.tpv45' - note neither name is used
274275
*
275276
* @since 2.0.1
277+
*
278+
* @deprecated Use `faker.internet.username()` instead.
276279
*/
277280
userName(
278281
options: {
@@ -289,6 +292,56 @@ export class InternetModule extends ModuleBase {
289292
*/
290293
lastName?: string;
291294
} = {}
295+
): string {
296+
deprecated({
297+
deprecated: 'faker.internet.userName()',
298+
proposed: 'faker.internet.username()',
299+
since: '9.1.0',
300+
until: '10.0.0',
301+
});
302+
303+
return this.username(options);
304+
}
305+
306+
/**
307+
* Generates a username using the given person's name as base.
308+
* The resulting username may use neither, one or both of the names provided.
309+
* This will always return a plain ASCII string.
310+
* Some basic stripping of accents and transliteration of characters will be done.
311+
*
312+
* @param options An options object.
313+
* @param options.firstName The optional first name to use. If not specified, a random one will be chosen.
314+
* @param options.lastName The optional last name to use. If not specified, a random one will be chosen.
315+
*
316+
* @see faker.internet.displayName(): For generating an Unicode display name.
317+
*
318+
* @example
319+
* faker.internet.username() // 'Nettie_Zboncak40'
320+
* faker.internet.username({ firstName: 'Jeanne' }) // 'Jeanne98'
321+
* faker.internet.username({ firstName: 'Jeanne' }) // 'Jeanne.Smith98'
322+
* faker.internet.username({ firstName: 'Jeanne', lastName: 'Doe'}) // 'Jeanne_Doe98'
323+
* faker.internet.username({ firstName: 'John', lastName: 'Doe' }) // 'John.Doe'
324+
* faker.internet.username({ firstName: 'Hélene', lastName: 'Müller' }) // 'Helene_Muller11'
325+
* faker.internet.username({ firstName: 'Фёдор', lastName: 'Достоевский' }) // 'Fedor.Dostoevskii50'
326+
* faker.internet.username({ firstName: '大羽', lastName: '陳' }) // 'hlzp8d.tpv45' - note neither name is used
327+
*
328+
* @since 9.1.0
329+
*/
330+
username(
331+
options: {
332+
/**
333+
* The optional first name to use.
334+
*
335+
* @default faker.person.firstName()
336+
*/
337+
firstName?: string;
338+
/**
339+
* The optional last name to use.
340+
*
341+
* @default faker.person.lastName()
342+
*/
343+
lastName?: string;
344+
} = {}
292345
): string {
293346
const {
294347
firstName = this.faker.person.firstName(),
@@ -348,7 +401,7 @@ export class InternetModule extends ModuleBase {
348401
* @param options.firstName The optional first name to use. If not specified, a random one will be chosen.
349402
* @param options.lastName The optional last name to use. If not specified, a random one will be chosen.
350403
*
351-
* @see faker.internet.userName(): For generating a plain ASCII username.
404+
* @see faker.internet.username(): For generating a plain ASCII username.
352405
*
353406
* @example
354407
* faker.internet.displayName() // 'Nettie_Zboncak40'

test/modules/__snapshots__/internet.spec.ts.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ exports[`internet > 42 > userName > with firstName option 1`] = `"Jane_Wiegand59
120120

121121
exports[`internet > 42 > userName > with lastName option 1`] = `"Garnet_Doe"`;
122122

123+
exports[`internet > 42 > username > noArgs 1`] = `"Garnet.Reynolds-Miller15"`;
124+
125+
exports[`internet > 42 > username > with Chinese names 1`] = `"hlzp8d.tpv"`;
126+
127+
exports[`internet > 42 > username > with Cyrillic names 1`] = `"Fedor.Dostoevskii"`;
128+
129+
exports[`internet > 42 > username > with Latin names 1`] = `"Jane.Doe"`;
130+
131+
exports[`internet > 42 > username > with accented names 1`] = `"Helene.Muller"`;
132+
133+
exports[`internet > 42 > username > with all option 1`] = `"Jane.Doe"`;
134+
135+
exports[`internet > 42 > username > with firstName option 1`] = `"Jane_Wiegand59"`;
136+
137+
exports[`internet > 42 > username > with lastName option 1`] = `"Garnet_Doe"`;
138+
123139
exports[`internet > 1211 > color > noArgs 1`] = `"#77721c"`;
124140

125141
exports[`internet > 1211 > color > with all options 1`] = `"#a9a44e"`;
@@ -240,6 +256,22 @@ exports[`internet > 1211 > userName > with firstName option 1`] = `"Jane99"`;
240256

241257
exports[`internet > 1211 > userName > with lastName option 1`] = `"Tito_Doe"`;
242258

259+
exports[`internet > 1211 > username > noArgs 1`] = `"Tito67"`;
260+
261+
exports[`internet > 1211 > username > with Chinese names 1`] = `"hlzp8d_tpv89"`;
262+
263+
exports[`internet > 1211 > username > with Cyrillic names 1`] = `"Fedor_Dostoevskii89"`;
264+
265+
exports[`internet > 1211 > username > with Latin names 1`] = `"Jane_Doe89"`;
266+
267+
exports[`internet > 1211 > username > with accented names 1`] = `"Helene_Muller89"`;
268+
269+
exports[`internet > 1211 > username > with all option 1`] = `"Jane_Doe89"`;
270+
271+
exports[`internet > 1211 > username > with firstName option 1`] = `"Jane99"`;
272+
273+
exports[`internet > 1211 > username > with lastName option 1`] = `"Tito_Doe"`;
274+
243275
exports[`internet > 1337 > color > noArgs 1`] = `"#211423"`;
244276

245277
exports[`internet > 1337 > color > with all options 1`] = `"#534655"`;
@@ -359,3 +391,19 @@ exports[`internet > 1337 > userName > with all option 1`] = `"Jane.Doe15"`;
359391
exports[`internet > 1337 > userName > with firstName option 1`] = `"Jane.Cronin45"`;
360392

361393
exports[`internet > 1337 > userName > with lastName option 1`] = `"Devyn.Doe27"`;
394+
395+
exports[`internet > 1337 > username > noArgs 1`] = `"Devyn.Gottlieb"`;
396+
397+
exports[`internet > 1337 > username > with Chinese names 1`] = `"hlzp8d.tpv15"`;
398+
399+
exports[`internet > 1337 > username > with Cyrillic names 1`] = `"Fedor.Dostoevskii15"`;
400+
401+
exports[`internet > 1337 > username > with Latin names 1`] = `"Jane.Doe15"`;
402+
403+
exports[`internet > 1337 > username > with accented names 1`] = `"Helene.Muller15"`;
404+
405+
exports[`internet > 1337 > username > with all option 1`] = `"Jane.Doe15"`;
406+
407+
exports[`internet > 1337 > username > with firstName option 1`] = `"Jane.Cronin45"`;
408+
409+
exports[`internet > 1337 > username > with lastName option 1`] = `"Devyn.Doe27"`;

test/modules/internet.spec.ts

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ describe('internet', () => {
6666
.it('with Chinese names', { firstName: '大羽', lastName: '陳' });
6767
});
6868

69+
t.describe('username', (t) => {
70+
t.it('noArgs')
71+
.it('with firstName option', { firstName: 'Jane' })
72+
.it('with lastName option', { lastName: 'Doe' })
73+
.it('with all option', { firstName: 'Jane', lastName: 'Doe' })
74+
.it('with Latin names', { firstName: 'Jane', lastName: 'Doe' })
75+
.it('with accented names', { firstName: 'Hélene', lastName: 'Müller' })
76+
.it('with Cyrillic names', {
77+
firstName: 'Фёдор',
78+
lastName: 'Достоевский',
79+
})
80+
.it('with Chinese names', { firstName: '大羽', lastName: '陳' });
81+
});
82+
6983
t.describe('displayName', (t) => {
7084
t.it('noArgs')
7185
.it('with firstName option', { firstName: 'Jane' })
@@ -347,16 +361,88 @@ describe('internet', () => {
347361
});
348362

349363
describe('userName()', () => {
364+
it('should return a random userName', () => {
365+
// eslint-disable-next-line @typescript-eslint/no-deprecated
366+
const userName = faker.internet.userName();
367+
368+
expect(userName).toBeTruthy();
369+
expect(userName).toBeTypeOf('string');
370+
expect(userName).toMatch(/\w/);
371+
});
372+
373+
it('should return a random userName with given firstName', () => {
374+
// eslint-disable-next-line @typescript-eslint/no-deprecated
375+
const userName = faker.internet.userName({ firstName: 'Aiden' });
376+
377+
expect(userName).toBeTruthy();
378+
expect(userName).toBeTypeOf('string');
379+
expect(userName).toMatch(/\w/);
380+
expect(userName).includes('Aiden');
381+
});
382+
383+
it('should return a random userName with given firstName and lastName', () => {
384+
// eslint-disable-next-line @typescript-eslint/no-deprecated
385+
const userName = faker.internet.userName({
386+
firstName: 'Aiden',
387+
lastName: 'Harann',
388+
});
389+
390+
expect(userName).toBeTruthy();
391+
expect(userName).toBeTypeOf('string');
392+
expect(userName).includes('Aiden');
393+
expect(userName).includes('Harann');
394+
expect(userName).toMatch(/^Aiden[._]Harann\d*/);
395+
});
396+
397+
it('should strip accents', () => {
398+
// eslint-disable-next-line @typescript-eslint/no-deprecated
399+
const userName = faker.internet.userName({
400+
firstName: 'Adèle',
401+
lastName: 'Smith',
402+
});
403+
expect(userName).includes('Adele');
404+
expect(userName).includes('Smith');
405+
});
406+
407+
it('should transliterate Cyrillic', () => {
408+
// eslint-disable-next-line @typescript-eslint/no-deprecated
409+
const userName = faker.internet.userName({
410+
firstName: 'Амос',
411+
lastName: 'Васильев',
412+
});
413+
expect(userName).includes('Amos');
414+
});
415+
416+
it('should provide a fallback for Chinese etc', () => {
417+
// eslint-disable-next-line @typescript-eslint/no-deprecated
418+
const userName = faker.internet.userName({
419+
firstName: '大羽',
420+
lastName: '陳',
421+
});
422+
expect(userName).includes('hlzp8d');
423+
});
424+
425+
it('should provide a fallback special unicode characters', () => {
426+
// eslint-disable-next-line @typescript-eslint/no-deprecated
427+
const userName = faker.internet.userName({
428+
firstName: '🐼',
429+
lastName: '❤️',
430+
});
431+
expect(userName).includes('2qt8');
432+
});
433+
});
434+
435+
describe('username()', () => {
350436
it('should return a random username', () => {
351-
const username = faker.internet.userName();
437+
const username = faker.internet.username();
352438

353439
expect(username).toBeTruthy();
354440
expect(username).toBeTypeOf('string');
355441
expect(username).toMatch(/\w/);
356442
});
357443

358444
it('should return a random username with given firstName', () => {
359-
const username = faker.internet.userName({ firstName: 'Aiden' });
445+
const username = faker.internet.username({ firstName: 'Aiden' });
360446

361447
expect(username).toBeTruthy();
362448
expect(username).toBeTypeOf('string');
@@ -365,7 +451,7 @@ describe('internet', () => {
365451
});
366452

367453
it('should return a random username with given firstName and lastName', () => {
368-
const username = faker.internet.userName({
454+
const username = faker.internet.username({
369455
firstName: 'Aiden',
370456
lastName: 'Harann',
371457
});
@@ -378,7 +464,7 @@ describe('internet', () => {
378464
});
379465

380466
it('should strip accents', () => {
381-
const username = faker.internet.userName({
467+
const username = faker.internet.username({
382468
firstName: 'Adèle',
383469
lastName: 'Smith',
384470
});
@@ -387,23 +473,23 @@ describe('internet', () => {
387473
});
388474

389475
it('should transliterate Cyrillic', () => {
390-
const username = faker.internet.userName({
476+
const username = faker.internet.username({
391477
firstName: 'Амос',
392478
lastName: 'Васильев',
393479
});
394480
expect(username).includes('Amos');
395481
});
396482

397483
it('should provide a fallback for Chinese etc', () => {
398-
const username = faker.internet.userName({
484+
const username = faker.internet.username({
399485
firstName: '大羽',
400486
lastName: '陳',
401487
});
402488
expect(username).includes('hlzp8d');
403489
});
404490

405491
it('should provide a fallback special unicode characters', () => {
406-
const username = faker.internet.userName({
492+
const username = faker.internet.username({
407493
firstName: '🐼',
408494
lastName: '❤️',
409495
});

test/scripts/apidocs/verify-jsdoc-tags.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ function resolvePathToMethodFile(
3636
signature: number
3737
): string {
3838
const dir = resolveDirToModule(moduleName);
39+
// TODO @ST-DDT 2024-09-23: Remove this in v10
40+
if (methodName === 'userName') {
41+
methodName = 'userNameDeprecated';
42+
}
43+
3944
return resolve(dir, `${methodName}_${signature}.ts`);
4045
}
4146

0 commit comments

Comments
 (0)