From a9433537c4658b507969b0cd1409d6652569ce6d Mon Sep 17 00:00:00 2001 From: DivisionByZero Date: Tue, 14 Mar 2023 20:30:33 +0100 Subject: [PATCH] refactor(internet): standardize signatures (#1845) Co-authored-by: ST-DDT Co-authored-by: Shinigami --- docs/guide/usage.md | 2 +- src/modules/git/index.ts | 4 +- src/modules/internet/index.ts | 974 ++++++++++++++++++++++- test/__snapshots__/internet.spec.ts.snap | 234 +++++- test/internet.spec.ts | 207 ++++- 5 files changed, 1318 insertions(+), 103 deletions(-) diff --git a/docs/guide/usage.md b/docs/guide/usage.md index 7448a699cc8..48a84fb1e96 100644 --- a/docs/guide/usage.md +++ b/docs/guide/usage.md @@ -148,7 +148,7 @@ function createRandomUser(): User { const sex = faker.person.sexType(); const firstName = faker.person.firstName(sex); const lastName = faker.person.lastName(); - const email = faker.internet.email(firstName, lastName); + const email = faker.internet.email({ firstName, lastName }); return { _id: faker.datatype.uuid(), diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index bb0bca0990f..e35ece2b435 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -115,9 +115,9 @@ export class GitModule { const firstName = this.faker.person.firstName(); const lastName = this.faker.person.lastName(); const fullName = this.faker.person.fullName({ firstName, lastName }); - const username = this.faker.internet.userName(firstName, lastName); + const username = this.faker.internet.userName({ firstName, lastName }); let user = this.faker.helpers.arrayElement([fullName, username]); - const email = this.faker.internet.email(firstName, lastName); + const email = this.faker.internet.email({ firstName, lastName }); // Normalize user according to https://github.com/libgit2/libgit2/issues/5342 user = user.replace(/^[\.,:;"\\']|[\<\>\n]|[\.,:;"\\']$/g, ''); diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index c0355209b86..75bebae051d 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { deprecated } from '../../internal/deprecated'; import { charMapping } from './char-mappings'; import * as random_ua from './user-agent'; @@ -55,6 +56,48 @@ export class InternetModule { )}.jpg`; } + /** + * Generates an email address using the given person's name as base. + * + * @param options The options to use. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param options.provider The mail provider domain to use. If not specified, a random free mail provider will be chosen. + * @param options.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * + * @example + * faker.internet.email() // 'Kassandra4@hotmail.com' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe' }) // 'Jeanne63@yahoo.com' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe', provider: 'example.fakerjs.dev' }) // 'Jeanne_Doe88@example.fakerjs.dev' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe', provider: 'example.fakerjs.dev', allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.fakerjs.dev' + * + * @since 2.0.1 + */ + email(options?: { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * The mail provider domain to use. If not specified, a random free mail provider will be chosen. + */ + provider?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + }): string; /** * Generates an email address using the given person's name as base. * @@ -72,6 +115,8 @@ export class InternetModule { * faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.fakerjs.dev' * * @since 2.0.1 + * + * @deprecated Use `faker.internet.email({ firstName, lastName, provider, ... })` instead. */ email( firstName?: string, @@ -85,14 +130,137 @@ export class InternetModule { */ allowSpecialCharacters?: boolean; } + ): string; + /** + * Generates an email address using the given person's name as base. + * + * @param options The options to use. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param options.provider The mail provider domain to use. If not specified, a random free mail provider will be chosen. + * @param options.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * @param legacyLastName The optional last name to use. If not specified, a random one will be chosen. + * @param legacyProvider The mail provider domain to use. If not specified, a random free mail provider will be chosen. + * @param legacyOptions The options to use. Defaults to `{ allowSpecialCharacters: false }`. + * @param legacyOptions.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * + * @example + * faker.internet.email() // 'Kassandra4@hotmail.com' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe' }) // 'Jeanne63@yahoo.com' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe', provider: 'example.fakerjs.dev' }) // 'Jeanne_Doe88@example.fakerjs.dev' + * faker.internet.email({ firstName: 'Jeanne', lastName: 'Doe', provider: 'example.fakerjs.dev', allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.fakerjs.dev' + * + * @since 2.0.1 + */ + email( + options?: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * The mail provider domain to use. If not specified, a random free mail provider will be chosen. + */ + provider?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + }, + legacyLastName?: string, + legacyProvider?: string, + legacyOptions?: { + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } + ): string; + email( + options: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * The mail provider domain to use. If not specified, a random free mail provider will be chosen. + */ + provider?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } = {}, + legacyLastName?: string, + legacyProvider?: string, + legacyOptions?: { + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } ): string { - provider = - provider || - this.faker.helpers.arrayElement( - this.faker.definitions.internet.free_email - ); + if ( + typeof options === 'string' || + legacyLastName != null || + legacyProvider != null || + legacyOptions != null + ) { + deprecated({ + deprecated: + 'faker.internet.email(firstName, lastName, provider, options)', + proposed: + 'faker.internet.email({ firstName, lastName, provider, ... })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'string') { + options = { firstName: options }; + } + + const { + firstName = this.faker.person.firstName(), + lastName = legacyLastName ?? this.faker.person.lastName(), + provider = legacyProvider ?? + this.faker.helpers.arrayElement( + this.faker.definitions.internet.free_email + ), + allowSpecialCharacters = legacyOptions?.allowSpecialCharacters ?? false, + } = options; - let localPart: string = this.userName(firstName, lastName); + let localPart: string = this.userName({ firstName, lastName }); // Strip any special characters from the local part of the email address // This could happen if invalid chars are passed in manually in the firstName/lastName localPart = localPart.replace(/[^A-Za-z0-9._+\-]+/g, ''); @@ -100,7 +268,7 @@ export class InternetModule { // The local part of an email address is limited to 64 chars per RFC 3696 // We limit to 50 chars to be more realistic localPart = localPart.substring(0, 50); - if (options?.allowSpecialCharacters) { + if (allowSpecialCharacters) { const usernameChars: string[] = '._-'.split(''); const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split(''); localPart = localPart.replace( @@ -119,6 +287,42 @@ export class InternetModule { return `${localPart}@${provider}`; } + /** + * Generates an email address using an example mail provider using the given person's name as base. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param options.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * + * @example + * faker.internet.exampleEmail() // 'Helmer.Graham23@example.com' + * faker.internet.exampleEmail({ firstName: 'Jeanne', lastName: 'Doe' }) // 'Jeanne96@example.net' + * faker.internet.exampleEmail({ firstName: 'Jeanne', lastName: 'Doe', allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.com' + * + * @since 3.1.0 + */ + exampleEmail(options?: { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + }): string; /** * Generates an email address using an example mail provider using the given person's name as base. * @@ -134,6 +338,8 @@ export class InternetModule { * faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.com' * * @since 3.1.0 + * + * @deprecated Use `faker.internet.exampleEmail({ firstName: lastName, ... })` instead. */ exampleEmail( firstName?: string, @@ -146,15 +352,169 @@ export class InternetModule { */ allowSpecialCharacters?: boolean; } + ): string; + /** + * Generates an email address using an example mail provider using the given person's name as base. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param options.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * @param legacyLastName The optional last name to use. If not specified, a random one will be chosen. + * @param legacyOptions The options to use. Defaults to `{}`. + * @param legacyOptions.allowSpecialCharacters Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included + * in the email address. Defaults to `false`. + * + * @example + * faker.internet.exampleEmail() // 'Helmer.Graham23@example.com' + * faker.internet.exampleEmail({ firstName: 'Jeanne', lastName: 'Doe' }) // 'Jeanne96@example.net' + * faker.internet.exampleEmail({ firstName: 'Jeanne', lastName: 'Doe', allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.com' + * + * @since 3.1.0 + */ + exampleEmail( + options?: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + }, + legacyLastName?: string, + legacyOptions?: { + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } + ): string; + exampleEmail( + options: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } = {}, + legacyLastName?: string, + legacyOptions?: { + /** + * Whether special characters such as ``.!#$%&'*+-/=?^_`{|}~`` should be included in the email address. + * + * @default false + */ + allowSpecialCharacters?: boolean; + } ): string { + if ( + typeof options === 'string' || + legacyLastName != null || + legacyOptions != null + ) { + deprecated({ + deprecated: 'faker.internet.exampleEmail(firstName, lastName, options)', + proposed: 'faker.internet.exampleEmail({ firstName, lastName, ... })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'string') { + options = { firstName: options }; + } + + const { + firstName = this.faker.person.firstName(), + lastName = legacyLastName ?? this.faker.person.lastName(), + allowSpecialCharacters = legacyOptions?.allowSpecialCharacters ?? false, + } = options; + const provider = this.faker.helpers.arrayElement( this.faker.definitions.internet.example_email ); - return this.email(firstName, lastName, provider, options); + + return this.email({ + firstName, + lastName, + provider, + allowSpecialCharacters, + }); } /** - * Generates a username using the given person's name as base. The resuling username may use neither, one or both of the names provided. This will always return a plain ASCII string. Some basic stripping of accents and transliteration of characters will be done. + * Generates a username using the given person's name as base. + * The resuling username may use neither, one or both of the names provided. + * This will always return a plain ASCII string. + * Some basic stripping of accents and transliteration of characters will be done. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * + * @see faker.internet.displayName() + * + * @example + * faker.internet.userName() // 'Nettie_Zboncak40' + * faker.internet.userName({ firstName: 'Jeanne', lastName: 'Doe'}) // 'Jeanne98' - note surname is not used + * faker.internet.userName({ firstName: 'John', lastName: 'Doe' }) // 'John.Doe' + * faker.internet.userName({ firstName: 'Hélene', lastName: 'Müller' }) // 'Helene_Muller11' + * faker.internet.userName({ firstName: 'Фёдор', lastName: 'Достоевский' }) // 'Fedor.Dostoevskii50' + * faker.internet.userName({ firstName: '大羽', lastName: '陳' }) // 'hlzp8d.tpv45' - note neither name is used + * + * @since 2.0.1 + */ + userName(options?: { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + }): string; + /** + * Generates a username using the given person's name as base. + * The resuling username may use neither, one or both of the names provided. + * This will always return a plain ASCII string. + * Some basic stripping of accents and transliteration of characters will be done. * * @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. @@ -170,11 +530,90 @@ export class InternetModule { * faker.internet.userName('大羽', '陳') // 'hlzp8d.tpv45' - note neither name is used * * @since 2.0.1 + * + * @deprecated Use `faker.internet.userName({ firstName, lastName })` instead. + */ + userName(firstName?: string, lastName?: string): string; + /** + * Generates a username using the given person's name as base. + * The resuling username may use neither, one or both of the names provided. + * This will always return a plain ASCII string. + * Some basic stripping of accents and transliteration of characters will be done. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param legacyLastName The optional last name to use. If not specified, a random one will be chosen. + * + * @see faker.internet.displayName() + * + * @example + * faker.internet.userName() // 'Nettie_Zboncak40' + * faker.internet.userName({ firstName: 'Jeanne', lastName: 'Doe'}) // 'Jeanne98' - note surname is not used + * faker.internet.userName({ firstName: 'John', lastName: 'Doe' }) // 'John.Doe' + * faker.internet.userName({ firstName: 'Hélene', lastName: 'Müller' }) // 'Helene_Muller11' + * faker.internet.userName({ firstName: 'Фёдор', lastName: 'Достоевский' }) // 'Fedor.Dostoevskii50' + * faker.internet.userName({ firstName: '大羽', lastName: '陳' }) // 'hlzp8d.tpv45' - note neither name is used + * + * @since 2.0.1 */ - userName(firstName?: string, lastName?: string): string { + userName( + options?: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + }, + legacyLastName?: string + ): string; + userName( + options: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + } = {}, + legacyLastName?: string + ): string { + if (typeof options === 'string' || legacyLastName != null) { + deprecated({ + deprecated: 'faker.internet.userName(firstName, lastName)', + proposed: 'faker.internet.userName({ firstName, lastName })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'string') { + options = { firstName: options }; + } + + const { + firstName = this.faker.person.firstName(), + lastName = legacyLastName ?? this.faker.person.lastName(), + } = options; + let result: string; - firstName = firstName || this.faker.person.firstName(); - lastName = lastName || this.faker.person.lastName(); switch (this.faker.number.int(2)) { case 0: result = `${firstName}${this.faker.number.int(99)}`; @@ -221,7 +660,46 @@ export class InternetModule { } /** - * Generates a display name using the given person's name as base. The resulting display name may use one or both of the provided names. If the input names include Unicode characters, the resulting display name will contain Unicode characters. It will not contain spaces. + * Generates a display name using the given person's name as base. + * The resulting display name may use one or both of the provided names. + * If the input names include Unicode characters, the resulting display name will contain Unicode characters. + * It will not contain spaces. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * + * @see faker.internet.userName() + * + * @example + * faker.internet.displayName() // 'Nettie_Zboncak40' + * faker.internet.displayName({ firstname 'Jeanne', lastName: 'Doe' }) // 'Jeanne98' - note surname not used. + * faker.internet.displayName({ firstname 'John', lastName: 'Doe' }) // 'John.Doe' + * faker.internet.displayName({ firstname 'Hélene', lastName: 'Müller' }) // 'Hélene_Müller11' + * faker.internet.displayName({ firstname 'Фёдор', lastName: 'Достоевский' }) // 'Фёдор.Достоевский50' + * faker.internet.displayName({ firstname '大羽', lastName: '陳' }) // '大羽.陳' + * + * @since 8.0.0 + */ + displayName(options?: { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + }): string; + /** + * Generates a display name using the given person's name as base. + * The resulting display name may use one or both of the provided names. + * If the input names include Unicode characters, the resulting display name will contain Unicode characters. + * It will not contain spaces. * * @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. @@ -230,18 +708,97 @@ export class InternetModule { * * @example * faker.internet.displayName() // 'Nettie_Zboncak40' - * faker.internet.displayName('Jeanne', 'Doe') // 'Jeanne98' - note surname not used. + * faker.internet.displayName('Jeanne', 'Doe') // 'Jeanne98' - note surname is not used * faker.internet.displayName('John', 'Doe') // 'John.Doe' * faker.internet.displayName('Hélene', 'Müller') // 'Hélene_Müller11' * faker.internet.displayName('Фёдор', 'Достоевский') // 'Фёдор.Достоевский50' * faker.internet.displayName('大羽', '陳') // '大羽.陳' * * @since 8.0.0 + * + * @deprecated Use `faker.internet.displayName({ firstName, lastName })` instead. */ - displayName(firstName?: string, lastName?: string): string { + displayName(firstName?: string, lastName?: string): string; + /** + * Generates a display name using the given person's name as base. + * The resulting display name may use one or both of the provided names. + * If the input names include Unicode characters, the resulting display name will contain Unicode characters. + * It will not contain spaces. + * + * @param options An options object. Defaults to `{}`. + * @param options.firstName The optional first name to use. If not specified, a random one will be chosen. + * @param options.lastName The optional last name to use. If not specified, a random one will be chosen. + * @param legacyLastName The optional last name to use. If not specified, a random one will be chosen. + * + * @see faker.internet.userName() + * + * @example + * faker.internet.displayName() // 'Nettie_Zboncak40' + * faker.internet.displayName({ firstName: 'Jeanne', lastName: 'Doe'}) // 'Jeanne98' - note surname is not used + * faker.internet.displayName({ firstName: 'John', lastName: 'Doe' }) // 'John.Doe' + * faker.internet.displayName({ firstName: 'Hélene', lastName: 'Müller' }) // 'Hélene_Müller11' + * faker.internet.displayName({ firstName: 'Фёдор', lastName: 'Достоевский' }) // 'Фёдор.Достоевский50' + * faker.internet.displayName({ firstName: '大羽', lastName: '陳' }) // '大羽.陳' + * + * @since 8.0.0 + */ + displayName( + options?: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + }, + legacyLastName?: string + ): string; + displayName( + options: + | string + | { + /** + * The optional first name to use. + * + * @default faker.person.firstName() + */ + firstName?: string; + /** + * The optional last name to use. + * + * @default faker.person.lastName() + */ + lastName?: string; + } = {}, + legacyLastName?: string + ): string { + if (typeof options === 'string' || legacyLastName != null) { + deprecated({ + deprecated: 'faker.internet.displayName(firstName, lastName)', + proposed: 'faker.internet.displayName({ firstName, lastName })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'string') { + options = { firstName: options }; + } + + const { + firstName = this.faker.person.firstName(), + lastName = legacyLastName ?? this.faker.person.lastName(), + } = options; + let result: string; - firstName = firstName || this.faker.person.firstName(); - lastName = lastName || this.faker.person.lastName(); switch (this.faker.number.int(2)) { case 0: result = `${firstName}${this.faker.number.int(99)}`; @@ -484,6 +1041,43 @@ export class InternetModule { return random_ua.generate(this.faker); } + /** + * Generates a random css hex color code in aesthetically pleasing color palette. + * + * Based on + * http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette + * + * @param options An options object. Defaults to `{}`. + * @param options.redBase The optional base red in range between `0` and `255`. Defaults to `0`. + * @param options.greenBase The optional base green in range between `0` and `255`. Defaults to `0`. + * @param options.blueBase The optional base blue in range between `0` and `255`. Defaults to `0`. + * + * @example + * faker.internet.color() // '#30686e' + * faker.internet.color({ redBase: 100, greenBase: 100, blueBase: 100 }) // '#4e5f8b' + * + * @since 2.0.1 + */ + color(options?: { + /** + * The optional base red in range between `0` and `255`. + * + * @default 0 + */ + redBase?: number; + /** + * The optional base green in range between `0` and `255`. + * + * @default 0 + */ + greenBase?: number; + /** + * The optional base blue in range between `0` and `255`. + * + * @default 0 + */ + blueBase?: number; + }): string; /** * Generates a random css hex color code in aesthetically pleasing color palette. * @@ -499,12 +1093,104 @@ export class InternetModule { * faker.internet.color(100, 100, 100) // '#4e5f8b' * * @since 2.0.1 + * + * @deprecated Use `faker.internet.color({ redbase, greenBase, blueBase })` instead. + */ + color(redBase?: number, greenBase?: number, blueBase?: number): string; + /** + * Generates a random css hex color code in aesthetically pleasing color palette. + * + * Based on + * http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette + * + * @param options An options object. Defaults to `{}`. + * @param options.redBase The optional base red in range between `0` and `255`. Defaults to `0`. + * @param options.greenBase The optional base green in range between `0` and `255`. Defaults to `0`. + * @param options.blueBase The optional base blue in range between `0` and `255`. Defaults to `0`. + * @param legacyGreenBase The optional base green in range between `0` and `255`. Defaults to `0`. + * @param legacyBlueBase The optional base blue in range between `0` and `255`. Defaults to `0`. + * + * @example + * faker.internet.color() // '#30686e' + * faker.internet.color({ redBase: 100, greenBase: 100, blueBase: 100 }) // '#4e5f8b' + * + * @since 2.0.1 */ color( - redBase: number = 0, - greenBase: number = 0, - blueBase: number = 0 + options?: + | number + | { + /** + * The optional base red in range between `0` and `255`. + * + * @default 0 + */ + redBase?: number; + /** + * The optional base green in range between `0` and `255`. + * + * @default 0 + */ + greenBase?: number; + /** + * The optional base blue in range between `0` and `255`. + * + * @default 0 + */ + blueBase?: number; + }, + legacyGreenBase?: number, + legacyBlueBase?: number + ): string; + color( + options: + | number + | { + /** + * The optional base red in range between `0` and `255`. + * + * @default 0 + */ + redBase?: number; + /** + * The optional base green in range between `0` and `255`. + * + * @default 0 + */ + greenBase?: number; + /** + * The optional base blue in range between `0` and `255`. + * + * @default 0 + */ + blueBase?: number; + } = {}, + legacyGreenBase?: number, + legacyBlueBase?: number ): string { + if ( + typeof options === 'number' || + legacyBlueBase != null || + legacyGreenBase != null + ) { + deprecated({ + deprecated: 'faker.internet.color(redBase, greenBase, blueBase)', + proposed: 'faker.internet.color({ redBase, greenBase, blueBase })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'number') { + options = { redBase: options }; + } + + const { + redBase = 0, + greenBase = legacyGreenBase ?? 0, + blueBase = legacyBlueBase ?? 0, + } = options; + const colorFromBase = (base: number): string => Math.floor((this.faker.number.int(256) + base) / 2) .toString(16) @@ -517,6 +1203,25 @@ export class InternetModule { return `#${red}${green}${blue}`; } + /** + * Generates a random mac address. + * + * @param options An options object. Defaults to `{}`. + * @param separator The optional separator to use. Can be either `':'`, `'-'` or `''`. Defaults to `':'`. + * + * @example + * faker.internet.mac() // '32:8e:2e:09:c6:05' + * + * @since 3.0.0 + */ + mac(options?: { + /** + * The optional separator to use. Can be either `':'`, `'-'` or `''`. + * + * @default ':' + */ + separator?: string; + }): string; /** * Generates a random mac address. * @@ -527,27 +1232,112 @@ export class InternetModule { * * @since 3.0.0 */ - mac(sep?: string): string { + mac(sep?: string): string; + /** + * Generates a random mac address. + * + * @param options The optional separator or an options object. Defaults to `{}`. + * @param separator The optional separator to use. Can be either `':'`, `'-'` or `''`. Defaults to `':'`. + * + * @example + * faker.internet.mac() // '32:8e:2e:09:c6:05' + * + * @since 3.0.0 + */ + mac( + options?: + | string + | { + /** + * The optional separator to use. Can be either `':'`, `'-'` or `''`. + * + * @default ':' + */ + separator?: string; + } + ): string; + mac( + options: + | string + | { + /** + * The optional separator to use. Can be either `':'`, `'-'` or `''`. + * + * @default ':' + */ + separator?: string; + } = {} + ): string { + if (typeof options === 'string') { + options = { separator: options }; + } + + let { separator = ':' } = options; + let i: number; let mac = ''; - let validSep = ':'; - // if the client passed in a different separator than `:`, - // we will use it if it is in the list of acceptable separators (dash or no separator) - if (['-', ''].indexOf(sep) !== -1) { - validSep = sep; + const acceptableSeparators = [':', '-', '']; + if (!acceptableSeparators.includes(separator)) { + separator = ':'; } for (i = 0; i < 12; i++) { mac += this.faker.number.hex(15); if (i % 2 === 1 && i !== 11) { - mac += validSep; + mac += separator; } } return mac; } + /** + * Generates a random password. + * + * @param options An options object. Defaults to `{}`. + * @param options.length The length of the password to generate. Defaults to `15`. + * @param options.memorable Whether the generated password should be memorable. Defaults to `false`. + * @param options.pattern The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. Defaults to `/\w/`. + * @param options.prefix The prefix to use. Defaults to `''`. + * + * @example + * faker.internet.password() // '89G1wJuBLbGziIs' + * faker.internet.password({ length: 20 }) // 'aF55c_8O9kZaPOrysFB_' + * faker.internet.password({ length: 20, memorable: true }) // 'lawetimufozujosodedi' + * faker.internet.password({ length: 20, memorable: true, pattern: /[A-Z]/ }) // 'HMAQDFFYLDDUTBKVNFVS' + * faker.internet.password({ length: 20, memorable: true, pattern: /[A-Z]/, prefix: 'Hello ' }) // 'Hello IREOXTDWPERQSB' + * + * @since 2.0.1 + */ + password(options?: { + /** + * The length of the password to generate. + * + * @default 15 + */ + length?: number; + /** + * Whether the generated password should be memorable. + * + * @default false + */ + memorable?: boolean; + /** + * The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. + * + * @default /\w/ + */ + pattern?: RegExp; + /** + * The prefix to use. + * + * @default '' + */ + prefix?: string; + }): string; /** * Generates a random password. * @@ -565,12 +1355,105 @@ export class InternetModule { * faker.internet.password(20, true, /[A-Z]/, 'Hello ') // 'Hello IREOXTDWPERQSB' * * @since 2.0.1 + * + * @deprecated Use `faker.internet({ length, memorable, pattern, prefix })` instead. + */ + password( + len?: number, + memorable?: boolean, + pattern?: RegExp, + prefix?: string + ): string; + /** + * Generates a random password. + * + * @param options The length of the password or an options object. Defaults to `{}`. + * @param options.length The length of the password to generate. Defaults to `15`. + * @param options.memorable Whether the generated password should be memorable. Defaults to `false`. + * @param options.pattern The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. Defaults to `/\w/`. + * @param options.prefix The prefix to use. Defaults to `''`. + * @param legacyMemorable Whether the generated password should be memorable. Defaults to `false`. + * @param legacyPattern The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. Defaults to `/\w/`. + * @param legacyPrefix The prefix to use. Defaults to `''`. + * + * @example + * faker.internet.password() // '89G1wJuBLbGziIs' + * faker.internet.password({ length: 20 }) // 'aF55c_8O9kZaPOrysFB_' + * faker.internet.password({ length: 20, memorable: true }) // 'lawetimufozujosodedi' + * faker.internet.password({ length: 20, memorable: true, pattern: /[A-Z]/ }) // 'HMAQDFFYLDDUTBKVNFVS' + * faker.internet.password({ length: 20, memorable: true, pattern: /[A-Z]/, prefix: 'Hello ' }) // 'Hello IREOXTDWPERQSB' + * + * @since 2.0.1 */ password( - len: number = 15, - memorable: boolean = false, - pattern: RegExp = /\w/, - prefix: string = '' + options?: + | number + | { + /** + * The length of the password to generate. + * + * @default 15 + */ + length?: number; + /** + * Whether the generated password should be memorable. + * + * @default false + */ + memorable?: boolean; + /** + * The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. + * + * @default /\w/ + */ + pattern?: RegExp; + /** + * The prefix to use. + * + * @default '' + */ + prefix?: string; + }, + legacyMemorable?: boolean, + legacyPattern?: RegExp, + legacyPrefix?: string + ): string; + password( + options: + | number + | { + /** + * The length of the password to generate. + * + * @default 15 + */ + length?: number; + /** + * Whether the generated password should be memorable. + * + * @default false + */ + memorable?: boolean; + /** + * The pattern that all chars should match should match. + * This option will be ignored, if `memorable` is `true`. + * + * @default /\w/ + */ + pattern?: RegExp; + /** + * The prefix to use. + * + * @default '' + */ + prefix?: string; + } = {}, + legacyMemorable?: boolean, + legacyPattern?: RegExp, + legacyPrefix?: string ): string { /* * password-generator ( function ) @@ -610,7 +1493,34 @@ export class InternetModule { return _password(length, memorable, pattern, prefix + char); }; - return _password(len, memorable, pattern, prefix); + if ( + typeof options === 'string' || + legacyMemorable != null || + legacyPattern != null || + legacyPrefix != null + ) { + deprecated({ + deprecated: + 'faker.internet.password(length, memorable, pattern, prefix)', + proposed: + 'faker.internet.password({ length, memorable, pattern, prefix })', + since: '8.0', + until: '9.0', + }); + } + + if (typeof options === 'number') { + options = { length: options }; + } + + const { + length = 15, + memorable = legacyMemorable ?? false, + pattern = legacyPattern ?? /\w/, + prefix = legacyPrefix ?? '', + } = options; + + return _password(length, memorable, pattern, prefix); } /** diff --git a/test/__snapshots__/internet.spec.ts.snap b/test/__snapshots__/internet.spec.ts.snap index 0273be665af..41106aa978c 100644 --- a/test/__snapshots__/internet.spec.ts.snap +++ b/test/__snapshots__/internet.spec.ts.snap @@ -4,7 +4,15 @@ exports[`internet > 42 > avatar 1`] = `"https://cloudflare-ipfs.com/ipfs/Qmd3W5D exports[`internet > 42 > color > noArgs 1`] = `"#30667a"`; -exports[`internet > 42 > color > with color base 1`] = `"#6298ac"`; +exports[`internet > 42 > color > with all options 1`] = `"#6298ac"`; + +exports[`internet > 42 > color > with blueBase option 1`] = `"#3066ac"`; + +exports[`internet > 42 > color > with greenBase option 1`] = `"#30987a"`; + +exports[`internet > 42 > color > with legacy color base 1`] = `"#6298ac"`; + +exports[`internet > 42 > color > with redBase option 1`] = `"#62667a"`; exports[`internet > 42 > displayName > noArgs 1`] = `"Garnet.Schinner73"`; @@ -16,27 +24,55 @@ exports[`internet > 42 > displayName > with Latin names 1`] = `"Jane_Doe"`; exports[`internet > 42 > displayName > with accented names 1`] = `"Hélene_Müller"`; +exports[`internet > 42 > displayName > with all option 1`] = `"Jane_Doe"`; + +exports[`internet > 42 > displayName > with firstName option 1`] = `"Jane_Hirthe18"`; + +exports[`internet > 42 > displayName > with lastName option 1`] = `"Garnet_Doe18"`; + +exports[`internet > 42 > displayName > with legacy names 1`] = `"Jane_Doe"`; + exports[`internet > 42 > domainName 1`] = `"hasty-sherbet.org"`; exports[`internet > 42 > domainSuffix 1`] = `"info"`; exports[`internet > 42 > domainWord 1`] = `"hasty-sherbet"`; -exports[`internet > 42 > email > noArgs 1`] = `"Peyton73@yahoo.com"`; +exports[`internet > 42 > email > noArgs 1`] = `"Garnet73@hotmail.com"`; + +exports[`internet > 42 > email > with all options 1`] = `"Jane_Doe@fakerjs.dev"`; + +exports[`internet > 42 > email > with allowSpecialCharacters option 1`] = `"Garnet73@hotmail.com"`; + +exports[`internet > 42 > email > with firstName option 1`] = `"Jane.Hirthe73@hotmail.com"`; + +exports[`internet > 42 > email > with lastName option 1`] = `"Garnet.Doe73@hotmail.com"`; -exports[`internet > 42 > email > with names 1`] = `"Jane_Doe18@yahoo.com"`; +exports[`internet > 42 > email > with legacy names 1`] = `"Jane_Doe18@yahoo.com"`; -exports[`internet > 42 > email > with names and provider 1`] = `"Jane_Doe@fakerjs.dev"`; +exports[`internet > 42 > email > with legacy names and provider 1`] = `"Jane_Doe@fakerjs.dev"`; -exports[`internet > 42 > email > with provider 1`] = `"Garnet.Schinner73@fakerjs.dev"`; +exports[`internet > 42 > email > with legacy provider 1`] = `"Garnet.Schinner73@fakerjs.dev"`; + +exports[`internet > 42 > email > with provider option 1`] = `"Garnet.Schinner73@fakerjs.dev"`; exports[`internet > 42 > emoji > noArgs 1`] = `"🕸️"`; exports[`internet > 42 > emoji > with options 1`] = `"🦔"`; -exports[`internet > 42 > exampleEmail > noArgs 1`] = `"Peyton73@example.com"`; +exports[`internet > 42 > exampleEmail > noArgs 1`] = `"Garnet73@example.net"`; + +exports[`internet > 42 > exampleEmail > with all options 1`] = `"Jane_Doe18@example.com"`; + +exports[`internet > 42 > exampleEmail > with allowSpecialCharacters option 1`] = `"Garnet73@example.net"`; + +exports[`internet > 42 > exampleEmail > with firstName option 1`] = `"Jane.Hirthe73@example.net"`; + +exports[`internet > 42 > exampleEmail > with lastName option 1`] = `"Garnet.Doe73@example.net"`; + +exports[`internet > 42 > exampleEmail > with legacy names 1`] = `"Jane_Doe18@example.com"`; -exports[`internet > 42 > exampleEmail > with names 1`] = `"Jane_Doe18@example.com"`; +exports[`internet > 42 > exampleEmail > with legacy names and options 1`] = `"Jane_Doe18@example.com"`; exports[`internet > 42 > httpMethod 1`] = `"POST"`; @@ -54,9 +90,27 @@ exports[`internet > 42 > mac > noArgs 1`] = `"5c:f2:bc:99:27:21"`; exports[`internet > 42 > mac > with separator 1`] = `"5c:f2:bc:99:27:21"`; +exports[`internet > 42 > mac > with separator option 1`] = `"5c-f2-bc-99-27-21"`; + exports[`internet > 42 > password > noArgs 1`] = `"Dl2fkYYKLsZdepz"`; -exports[`internet > 42 > password > with length 1`] = `"Dl2fkYYKLs"`; +exports[`internet > 42 > password > with legacy length 1`] = `"Dl2fkYYKLs"`; + +exports[`internet > 42 > password > with legacy length and memorable 1`] = `"Dl2fkYYKLs"`; + +exports[`internet > 42 > password > with legacy length, memorable and pattern 1`] = `"2522731671"`; + +exports[`internet > 42 > password > with legacy length, memorable, pattern and prefix 1`] = `"test252273"`; + +exports[`internet > 42 > password > with length option 1`] = `"Dl2fkYYKLs"`; + +exports[`internet > 42 > password > with length, memorable, pattern and prefix option 1`] = `"test252273"`; + +exports[`internet > 42 > password > with memorable option 1`] = `"Dl2fkYYKLsZdepz"`; + +exports[`internet > 42 > password > with pattern option 1`] = `"252273167192423"`; + +exports[`internet > 42 > password > with prefix option 1`] = `"testDl2fkYYKLsZ"`; exports[`internet > 42 > port 1`] = `24545`; @@ -80,11 +134,27 @@ exports[`internet > 42 > userName > with Latin names 1`] = `"Jane_Doe"`; exports[`internet > 42 > userName > with accented names 1`] = `"Helene_Muller"`; +exports[`internet > 42 > userName > with all option 1`] = `"Jane_Doe"`; + +exports[`internet > 42 > userName > with firstName option 1`] = `"Jane_Hirthe18"`; + +exports[`internet > 42 > userName > with lastName option 1`] = `"Garnet_Doe18"`; + +exports[`internet > 42 > userName > with legacy names 1`] = `"Jane_Doe"`; + exports[`internet > 1211 > avatar 1`] = `"https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1160.jpg"`; exports[`internet > 1211 > color > noArgs 1`] = `"#773a72"`; -exports[`internet > 1211 > color > with color base 1`] = `"#a96ca4"`; +exports[`internet > 1211 > color > with all options 1`] = `"#a96ca4"`; + +exports[`internet > 1211 > color > with blueBase option 1`] = `"#773aa4"`; + +exports[`internet > 1211 > color > with greenBase option 1`] = `"#776c72"`; + +exports[`internet > 1211 > color > with legacy color base 1`] = `"#a96ca4"`; + +exports[`internet > 1211 > color > with redBase option 1`] = `"#a93a72"`; exports[`internet > 1211 > displayName > noArgs 1`] = `"Tito_Koelpin22"`; @@ -96,27 +166,55 @@ exports[`internet > 1211 > displayName > with Latin names 1`] = `"Jane.Doe89"`; exports[`internet > 1211 > displayName > with accented names 1`] = `"Hélene.Müller89"`; +exports[`internet > 1211 > displayName > with all option 1`] = `"Jane.Doe89"`; + +exports[`internet > 1211 > displayName > with firstName option 1`] = `"Jane_Ward"`; + +exports[`internet > 1211 > displayName > with lastName option 1`] = `"Tito_Doe"`; + +exports[`internet > 1211 > displayName > with legacy names 1`] = `"Jane.Doe89"`; + exports[`internet > 1211 > domainName 1`] = `"vicious-infrastructure.org"`; exports[`internet > 1211 > domainSuffix 1`] = `"org"`; exports[`internet > 1211 > domainWord 1`] = `"vicious-infrastructure"`; -exports[`internet > 1211 > email > noArgs 1`] = `"Jadyn.Trantow12@hotmail.com"`; +exports[`internet > 1211 > email > noArgs 1`] = `"Tito.Koelpin12@hotmail.com"`; + +exports[`internet > 1211 > email > with all options 1`] = `"Jane.Doe89@fakerjs.dev"`; + +exports[`internet > 1211 > email > with allowSpecialCharacters option 1`] = `"Tito.Koelpin12@hotmail.com"`; + +exports[`internet > 1211 > email > with firstName option 1`] = `"Jane_Ward22@yahoo.com"`; -exports[`internet > 1211 > email > with names 1`] = `"Jane_Doe@hotmail.com"`; +exports[`internet > 1211 > email > with lastName option 1`] = `"Tito_Doe22@yahoo.com"`; -exports[`internet > 1211 > email > with names and provider 1`] = `"Jane.Doe89@fakerjs.dev"`; +exports[`internet > 1211 > email > with legacy names 1`] = `"Jane_Doe@hotmail.com"`; -exports[`internet > 1211 > email > with provider 1`] = `"Tito_Koelpin22@fakerjs.dev"`; +exports[`internet > 1211 > email > with legacy names and provider 1`] = `"Jane.Doe89@fakerjs.dev"`; + +exports[`internet > 1211 > email > with legacy provider 1`] = `"Tito_Koelpin22@fakerjs.dev"`; + +exports[`internet > 1211 > email > with provider option 1`] = `"Tito_Koelpin22@fakerjs.dev"`; exports[`internet > 1211 > emoji > noArgs 1`] = `"🇮🇸"`; exports[`internet > 1211 > emoji > with options 1`] = `"🌲"`; -exports[`internet > 1211 > exampleEmail > noArgs 1`] = `"Jadyn.Trantow12@example.net"`; +exports[`internet > 1211 > exampleEmail > noArgs 1`] = `"Tito.Koelpin12@example.net"`; + +exports[`internet > 1211 > exampleEmail > with all options 1`] = `"Jane_Doe@example.net"`; + +exports[`internet > 1211 > exampleEmail > with allowSpecialCharacters option 1`] = `"Tito.Koelpin12@example.net"`; + +exports[`internet > 1211 > exampleEmail > with firstName option 1`] = `"Jane_Ward22@example.com"`; -exports[`internet > 1211 > exampleEmail > with names 1`] = `"Jane_Doe@example.net"`; +exports[`internet > 1211 > exampleEmail > with lastName option 1`] = `"Tito_Doe22@example.com"`; + +exports[`internet > 1211 > exampleEmail > with legacy names 1`] = `"Jane_Doe@example.net"`; + +exports[`internet > 1211 > exampleEmail > with legacy names and options 1`] = `"Jane_Doe@example.net"`; exports[`internet > 1211 > httpMethod 1`] = `"PATCH"`; @@ -134,9 +232,27 @@ exports[`internet > 1211 > mac > noArgs 1`] = `"e7:ec:32:f0:a2:a3"`; exports[`internet > 1211 > mac > with separator 1`] = `"e7:ec:32:f0:a2:a3"`; +exports[`internet > 1211 > mac > with separator option 1`] = `"e7-ec-32-f0-a2-a3"`; + exports[`internet > 1211 > password > noArgs 1`] = `"yLuj60b5iHB0bhn"`; -exports[`internet > 1211 > password > with length 1`] = `"yLuj60b5iH"`; +exports[`internet > 1211 > password > with legacy length 1`] = `"yLuj60b5iH"`; + +exports[`internet > 1211 > password > with legacy length and memorable 1`] = `"yLuj60b5iH"`; + +exports[`internet > 1211 > password > with legacy length, memorable and pattern 1`] = `"6050530611"`; + +exports[`internet > 1211 > password > with legacy length, memorable, pattern and prefix 1`] = `"test605053"`; + +exports[`internet > 1211 > password > with length option 1`] = `"yLuj60b5iH"`; + +exports[`internet > 1211 > password > with length, memorable, pattern and prefix option 1`] = `"test605053"`; + +exports[`internet > 1211 > password > with memorable option 1`] = `"yLuj60b5iHB0bhn"`; + +exports[`internet > 1211 > password > with pattern option 1`] = `"605053061176844"`; + +exports[`internet > 1211 > password > with prefix option 1`] = `"testyLuj60b5iHB"`; exports[`internet > 1211 > port 1`] = `60851`; @@ -160,11 +276,27 @@ exports[`internet > 1211 > userName > with Latin names 1`] = `"Jane.Doe89"`; exports[`internet > 1211 > userName > with accented names 1`] = `"Helene.Muller89"`; +exports[`internet > 1211 > userName > with all option 1`] = `"Jane.Doe89"`; + +exports[`internet > 1211 > userName > with firstName option 1`] = `"Jane_Ward"`; + +exports[`internet > 1211 > userName > with lastName option 1`] = `"Tito_Doe"`; + +exports[`internet > 1211 > userName > with legacy names 1`] = `"Jane.Doe89"`; + exports[`internet > 1337 > avatar 1`] = `"https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/327.jpg"`; exports[`internet > 1337 > color > noArgs 1`] = `"#214814"`; -exports[`internet > 1337 > color > with color base 1`] = `"#537a46"`; +exports[`internet > 1337 > color > with all options 1`] = `"#537a46"`; + +exports[`internet > 1337 > color > with blueBase option 1`] = `"#214846"`; + +exports[`internet > 1337 > color > with greenBase option 1`] = `"#217a14"`; + +exports[`internet > 1337 > color > with legacy color base 1`] = `"#537a46"`; + +exports[`internet > 1337 > color > with redBase option 1`] = `"#534814"`; exports[`internet > 1337 > displayName > noArgs 1`] = `"Devyn21"`; @@ -176,27 +308,55 @@ exports[`internet > 1337 > displayName > with Latin names 1`] = `"Jane56"`; exports[`internet > 1337 > displayName > with accented names 1`] = `"Hélene56"`; +exports[`internet > 1337 > displayName > with all option 1`] = `"Jane56"`; + +exports[`internet > 1337 > displayName > with firstName option 1`] = `"Jane.Gibson"`; + +exports[`internet > 1337 > displayName > with lastName option 1`] = `"Devyn.Doe"`; + +exports[`internet > 1337 > displayName > with legacy names 1`] = `"Jane56"`; + exports[`internet > 1337 > domainName 1`] = `"fair-mile.com"`; exports[`internet > 1337 > domainSuffix 1`] = `"biz"`; exports[`internet > 1337 > domainWord 1`] = `"fair-mile"`; -exports[`internet > 1337 > email > noArgs 1`] = `"Kellen27@gmail.com"`; +exports[`internet > 1337 > email > noArgs 1`] = `"Devyn27@gmail.com"`; -exports[`internet > 1337 > email > with names 1`] = `"Jane.Doe@gmail.com"`; +exports[`internet > 1337 > email > with all options 1`] = `"Jane56@fakerjs.dev"`; -exports[`internet > 1337 > email > with names and provider 1`] = `"Jane56@fakerjs.dev"`; +exports[`internet > 1337 > email > with allowSpecialCharacters option 1`] = `"Devyn27@gmail.com"`; -exports[`internet > 1337 > email > with provider 1`] = `"Devyn21@fakerjs.dev"`; +exports[`internet > 1337 > email > with firstName option 1`] = `"Jane21@yahoo.com"`; + +exports[`internet > 1337 > email > with lastName option 1`] = `"Devyn21@yahoo.com"`; + +exports[`internet > 1337 > email > with legacy names 1`] = `"Jane.Doe@gmail.com"`; + +exports[`internet > 1337 > email > with legacy names and provider 1`] = `"Jane56@fakerjs.dev"`; + +exports[`internet > 1337 > email > with legacy provider 1`] = `"Devyn21@fakerjs.dev"`; + +exports[`internet > 1337 > email > with provider option 1`] = `"Devyn21@fakerjs.dev"`; exports[`internet > 1337 > emoji > noArgs 1`] = `"💇🏼‍♀️"`; exports[`internet > 1337 > emoji > with options 1`] = `"🐪"`; -exports[`internet > 1337 > exampleEmail > noArgs 1`] = `"Kellen27@example.org"`; +exports[`internet > 1337 > exampleEmail > noArgs 1`] = `"Devyn27@example.org"`; + +exports[`internet > 1337 > exampleEmail > with all options 1`] = `"Jane&Doe@example.org"`; -exports[`internet > 1337 > exampleEmail > with names 1`] = `"Jane.Doe@example.org"`; +exports[`internet > 1337 > exampleEmail > with allowSpecialCharacters option 1`] = `"Devyn27@example.org"`; + +exports[`internet > 1337 > exampleEmail > with firstName option 1`] = `"Jane21@example.com"`; + +exports[`internet > 1337 > exampleEmail > with lastName option 1`] = `"Devyn21@example.com"`; + +exports[`internet > 1337 > exampleEmail > with legacy names 1`] = `"Jane.Doe@example.org"`; + +exports[`internet > 1337 > exampleEmail > with legacy names and options 1`] = `"Jane&Doe@example.org"`; exports[`internet > 1337 > httpMethod 1`] = `"POST"`; @@ -214,9 +374,27 @@ exports[`internet > 1337 > mac > noArgs 1`] = `"48:23:48:70:53:89"`; exports[`internet > 1337 > mac > with separator 1`] = `"48:23:48:70:53:89"`; +exports[`internet > 1337 > mac > with separator option 1`] = `"48-23-48-70-53-89"`; + exports[`internet > 1337 > password > noArgs 1`] = `"9V05TL7RY9fmECg"`; -exports[`internet > 1337 > password > with length 1`] = `"9V05TL7RY9"`; +exports[`internet > 1337 > password > with legacy length 1`] = `"9V05TL7RY9"`; + +exports[`internet > 1337 > password > with legacy length and memorable 1`] = `"9V05TL7RY9"`; + +exports[`internet > 1337 > password > with legacy length, memorable and pattern 1`] = `"9057902132"`; + +exports[`internet > 1337 > password > with legacy length, memorable, pattern and prefix 1`] = `"test905790"`; + +exports[`internet > 1337 > password > with length option 1`] = `"9V05TL7RY9"`; + +exports[`internet > 1337 > password > with length, memorable, pattern and prefix option 1`] = `"test905790"`; + +exports[`internet > 1337 > password > with memorable option 1`] = `"9V05TL7RY9fmECg"`; + +exports[`internet > 1337 > password > with pattern option 1`] = `"905790213209301"`; + +exports[`internet > 1337 > password > with prefix option 1`] = `"test9V05TL7RY9f"`; exports[`internet > 1337 > port 1`] = `17172`; @@ -239,3 +417,11 @@ exports[`internet > 1337 > userName > with Cyrillic names 1`] = `"Fedor56"`; exports[`internet > 1337 > userName > with Latin names 1`] = `"Jane56"`; exports[`internet > 1337 > userName > with accented names 1`] = `"Helene56"`; + +exports[`internet > 1337 > userName > with all option 1`] = `"Jane56"`; + +exports[`internet > 1337 > userName > with firstName option 1`] = `"Jane.Gibson"`; + +exports[`internet > 1337 > userName > with lastName option 1`] = `"Devyn.Doe"`; + +exports[`internet > 1337 > userName > with legacy names 1`] = `"Jane56"`; diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 0730f384b62..53c25a8167f 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -24,33 +24,99 @@ describe('internet', () => { t.describe('email', (t) => { t.it('noArgs') - .it('with names', 'Jane', 'Doe') - .it('with provider', undefined, undefined, 'fakerjs.dev') - .it('with names and provider', 'Jane', 'Doe', 'fakerjs.dev'); + .it('with firstName option', { firstName: 'Jane' }) + .it('with lastName option', { lastName: 'Doe' }) + .it('with provider option', { provider: 'fakerjs.dev' }) + .it('with allowSpecialCharacters option', { + allowSpecialCharacters: true, + }) + .it('with all options', { + allowSpecialCharacters: true, + firstName: 'Jane', + lastName: 'Doe', + provider: 'fakerjs.dev', + }) + .it('with legacy names', 'Jane', 'Doe') + .it('with legacy provider', undefined, undefined, 'fakerjs.dev') + .it('with legacy names and provider', 'Jane', 'Doe', 'fakerjs.dev'); }); t.describe('exampleEmail', (t) => { - t.it('noArgs').it('with names', 'Jane', 'Doe'); + t.it('noArgs') + .it('with firstName option', { firstName: 'Jane' }) + .it('with lastName option', { lastName: 'Doe' }) + .it('with allowSpecialCharacters option', { + allowSpecialCharacters: true, + }) + .it('with all options', { + allowSpecialCharacters: true, + firstName: 'Jane', + lastName: 'Doe', + }) + .it('with legacy names', 'Jane', 'Doe') + .it('with legacy names and options', 'Jane', 'Doe', { + allowSpecialCharacters: true, + }); }); t.describe('userName', (t) => { t.it('noArgs') - .it('with Latin names', 'Jane', 'Doe') - .it('with accented names', 'Hélene', 'Müller') - .it('with Cyrillic names', 'Фёдор', 'Достоевский') - .it('with Chinese names', '大羽', '陳'); + .it('with firstName option', { firstName: 'Jane' }) + .it('with lastName option', { lastName: 'Doe' }) + .it('with all option', { firstName: 'Jane', lastName: 'Doe' }) + .it('with legacy names', 'Jane', 'Doe') + .it('with Latin names', { firstName: 'Jane', lastName: 'Doe' }) + .it('with accented names', { firstName: 'Hélene', lastName: 'Müller' }) + .it('with Cyrillic names', { + firstName: 'Фёдор', + lastName: 'Достоевский', + }) + .it('with Chinese names', { firstName: '大羽', lastName: '陳' }); }); t.describe('displayName', (t) => { t.it('noArgs') - .it('with Latin names', 'Jane', 'Doe') - .it('with accented names', 'Hélene', 'Müller') - .it('with Cyrillic names', 'Фёдор', 'Достоевский') - .it('with Chinese names', '大羽', '陳'); + .it('with firstName option', { firstName: 'Jane' }) + .it('with lastName option', { lastName: 'Doe' }) + .it('with all option', { firstName: 'Jane', lastName: 'Doe' }) + .it('with legacy names', 'Jane', 'Doe') + .it('with Latin names', { firstName: 'Jane', lastName: 'Doe' }) + .it('with accented names', { firstName: 'Hélene', lastName: 'Müller' }) + .it('with Cyrillic names', { + firstName: 'Фёдор', + lastName: 'Достоевский', + }) + .it('with Chinese names', { firstName: '大羽', lastName: '陳' }); }); t.describe('password', (t) => { - t.it('noArgs').it('with length', 10); + t.it('noArgs') + .it('with length option', { length: 10 }) + .it('with memorable option', { + memorable: false, + }) + .it('with pattern option', { + pattern: /[0-9]/, + }) + .it('with prefix option', { + prefix: 'test', + }) + .it('with length, memorable, pattern and prefix option', { + length: 10, + memorable: false, + pattern: /[0-9]/, + prefix: 'test', + }) + .it('with legacy length', 10) + .it('with legacy length and memorable', 10, false) + .it('with legacy length, memorable and pattern', 10, false, /[0-9]/) + .it( + 'with legacy length, memorable, pattern and prefix', + 10, + false, + /[0-9]/, + 'test' + ); }); t.describe('httpStatusCode', (t) => { @@ -58,11 +124,22 @@ describe('internet', () => { }); t.describe('color', (t) => { - t.it('noArgs').it('with color base', 100, 100, 100); + t.it('noArgs') + .it('with blueBase option', { blueBase: 100 }) + .it('with greenBase option', { greenBase: 100 }) + .it('with redBase option', { redBase: 100 }) + .it('with all options', { + redBase: 100, + blueBase: 100, + greenBase: 100, + }) + .it('with legacy color base', 100, 100, 100); }); t.describe('mac', (t) => { - t.it('noArgs').it('with separator', ':'); + t.it('noArgs') + .it('with separator', ':') + .it('with separator option', { separator: '-' }); }); t.describe('emoji', (t) => { @@ -118,7 +195,7 @@ describe('internet', () => { ); it('should return an email with given firstName', () => { - const email = faker.internet.email('Aiden.Harann55'); + const email = faker.internet.email({ firstName: 'Aiden.Harann55' }); expect(email).toBeTruthy(); expect(email).toBeTypeOf('string'); @@ -158,7 +235,10 @@ describe('internet', () => { }); it('should return an email with given firstName and lastName', () => { - const email = faker.internet.email('Aiden', 'Harann'); + const email = faker.internet.email({ + firstName: 'Aiden', + lastName: 'Harann', + }); expect(email).toBeTruthy(); expect(email).toBeTypeOf('string'); @@ -177,7 +257,10 @@ describe('internet', () => { const longFirstName = 'Elizabeth Alexandra Mary Jane Annabel Victoria'; const longSurname = 'Smith Jones Davidson Brown White Greene Black'; - const email = faker.internet.email(longFirstName, longSurname); + const email = faker.internet.email({ + firstName: longFirstName, + lastName: longSurname, + }); // should truncate to 50 chars // e.g. ElizabethAlexandraMaryJaneAnnabelVictoria.SmithJon@yahoo.com expect(email).toSatisfy(validator.isEmail); @@ -186,14 +269,19 @@ describe('internet', () => { }); it('should return a valid email for names with invalid chars', () => { - const email = faker.internet.email('Matthew (Matt)', 'Smith'); + const email = faker.internet.email({ + firstName: 'Matthew (Matt)', + lastName: 'Smith', + }); // should strip invalid chars // e.g. MatthewMatt_Smith@yahoo.com expect(email).toSatisfy(validator.isEmail); }); it('should return an email with special characters', () => { - const email = faker.internet.email('Mike', 'Smith', null, { + const email = faker.internet.email({ + firstName: 'Mike', + lastName: 'Smith', allowSpecialCharacters: true, }); @@ -225,7 +313,9 @@ describe('internet', () => { }); it('should return an email with the example suffix and given firstName', () => { - const email = faker.internet.exampleEmail('Aiden.Harann55'); + const email = faker.internet.exampleEmail({ + firstName: 'Aiden.Harann55', + }); expect(email).toBeTruthy(); expect(email).toBeTypeOf('string'); @@ -239,7 +329,10 @@ describe('internet', () => { }); it('should return an email with the example suffix and given firstName and lastName', () => { - const email = faker.internet.exampleEmail('Aiden', 'Harann'); + const email = faker.internet.exampleEmail({ + firstName: 'Aiden', + lastName: 'Harann', + }); expect(email).toBeTruthy(); expect(email).toBeTypeOf('string'); @@ -253,7 +346,9 @@ describe('internet', () => { }); it('should return an email with special characters', () => { - const email = faker.internet.exampleEmail('Mike', 'Smith', { + const email = faker.internet.exampleEmail({ + firstName: 'Mike', + lastName: 'Smith', allowSpecialCharacters: true, }); @@ -279,7 +374,7 @@ describe('internet', () => { }); it('should return a random username with given firstName', () => { - const username = faker.internet.userName('Aiden'); + const username = faker.internet.userName({ firstName: 'Aiden' }); expect(username).toBeTruthy(); expect(username).toBeTypeOf('string'); @@ -288,7 +383,10 @@ describe('internet', () => { }); it('should return a random username with given firstName and lastName', () => { - const username = faker.internet.userName('Aiden', 'Harann'); + const username = faker.internet.userName({ + firstName: 'Aiden', + lastName: 'Harann', + }); expect(username).toBeTruthy(); expect(username).toBeTypeOf('string'); @@ -299,12 +397,18 @@ describe('internet', () => { }); it('should strip accents', () => { - const username = faker.internet.userName('Adèle', 'Smith'); + const username = faker.internet.userName({ + firstName: 'Adèle', + lastName: 'Smith', + }); expect(username).includes('Adele'); }); it('should transliterate Cyrillic', () => { - const username = faker.internet.userName('Амос', 'Васильев'); + const username = faker.internet.userName({ + firstName: 'Амос', + lastName: 'Васильев', + }); expect(username).includes('Amos'); }); @@ -324,7 +428,9 @@ describe('internet', () => { }); it('should return a random display name with given firstName', () => { - const displayName = faker.internet.displayName('Aiden'); + const displayName = faker.internet.displayName({ + firstName: 'Aiden', + }); expect(displayName).toBeTruthy(); expect(displayName).toBeTypeOf('string'); @@ -333,7 +439,10 @@ describe('internet', () => { }); it('should return a random display name with given firstName and lastName', () => { - const displayName = faker.internet.displayName('Aiden', 'Harann'); + const displayName = faker.internet.displayName({ + firstName: 'Aiden', + lastName: 'Harann', + }); expect(displayName).toBeTruthy(); expect(displayName).toBeTypeOf('string'); @@ -550,7 +659,11 @@ describe('internet', () => { }); it('should return a random hex value with given values', () => { - const color = faker.internet.color(100, 100, 100); + const color = faker.internet.color({ + redBase: 100, + greenBase: 100, + blueBase: 100, + }); expect(color).toBeTruthy(); expect(color).toBeTypeOf('string'); @@ -616,7 +729,7 @@ describe('internet', () => { it.each(times(32))( 'should return random password with length %i', (length) => { - const password = faker.internet.password(length); + const password = faker.internet.password({ length }); expect(password).toBeTruthy(); expect(password).toBeTypeOf('string'); @@ -626,7 +739,10 @@ describe('internet', () => { ); it('should return memorable password', () => { - const password = faker.internet.password(12, true); + const password = faker.internet.password({ + length: 12, + memorable: true, + }); expect(password).toBeTruthy(); expect(password).toBeTypeOf('string'); @@ -635,7 +751,10 @@ describe('internet', () => { }); it('should return non memorable password', () => { - const password = faker.internet.password(12, false); + const password = faker.internet.password({ + length: 12, + memorable: false, + }); expect(password).toBeTruthy(); expect(password).toBeTypeOf('string'); @@ -646,11 +765,11 @@ describe('internet', () => { }); it('should return non memorable strong password with length 32', () => { - const password = faker.internet.password( - 32, - false, - /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/ - ); + const password = faker.internet.password({ + length: 32, + memorable: false, + pattern: /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/, + }); expect(password).toBeTruthy(); expect(password).toBeTypeOf('string'); @@ -660,12 +779,12 @@ describe('internet', () => { }); it('should return non memorable strong password with length 32 and given prefix', () => { - const password = faker.internet.password( - 32, - false, - /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/, - 'a!G6' - ); + const password = faker.internet.password({ + length: 32, + memorable: false, + pattern: /(!|\?|&|\[|\]|%|\$|[a-zA-Z0-9])/, + prefix: 'a!G6', + }); expect(password).toBeTruthy(); expect(password).toBeTypeOf('string');