From 09e835664add0a342dd089f31bbae7d880198493 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Wed, 26 Oct 2022 16:42:17 -0400 Subject: feat(internet): add options to url() (#1480) --- src/modules/internet/index.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/modules') diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 1c7e85cc..60e3ac55 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -20,6 +20,8 @@ export type HTTPStatusCodeType = | 'serverError' | 'redirection'; +export type HTTPProtocolType = 'http' | 'https'; + /** * Module to generate internet related entries. */ @@ -226,15 +228,28 @@ export class InternetModule { } /** - * Generates a random url. + * Generates a random http(s) url. + * + * @param options Optional options object. + * @param options.appendSlash Whether to append a slash to the end of the url (path). Defaults to a random boolean value. + * @param options.protocol The protocol to use. Defaults to `'https'`. * * @example * faker.internet.url() // 'https://remarkable-hackwork.info' + * faker.internet.url({ appendSlash: true }) // 'https://slow-timer.info/' + * faker.internet.url({ protocol: 'http', appendSlash: false }) // 'http://www.terrible-idea.com' * * @since 2.1.5 */ - url(): string { - return `${this.protocol()}://${this.domainName()}`; + url( + options: { + appendSlash?: boolean; + protocol?: HTTPProtocolType; + } = {} + ): string { + const { appendSlash = this.faker.datatype.boolean(), protocol = 'https' } = + options; + return `${protocol}://${this.domainName()}${appendSlash ? '/' : ''}`; } /** -- cgit v1.2.3