Repository: withastro/astro
File: packages/internal-helpers/src/remote.ts (TypeScript)

Code before:
	pathname?: string;
	protocol?: string;
	port?: string;
};

/**
 * Evaluates whether a given URL matches the specified remote pattern based on protocol, hostname, port, and pathname.
 *
 * @param {URL} url - The URL object to be matched against the remote pattern.
 * @param {RemotePattern} remotePattern - The remote pattern object containing the protocol, hostname, port, and pathname to match.
 * @return {boolean} Returns `true` if the URL matches the given remote pattern; otherwise, `false`.
 */
export function matchPattern(url: URL, remotePattern: RemotePattern): boolean {
	return (
		matchProtocol(url, remotePattern.protocol) &&
		matchHostname(url, remotePattern.hostname, true) &&
		matchPort(url, remotePattern.port) &&
		matchPathname(url, remotePattern.pathname, true)
	);
}

/**
 * Checks if the given URL's port matches the specified port. If no port is provided, it returns true.
 *
 * @param {URL} url - The URL object whose port will be checked.
 * @param {string} [port] - The port to match against the URL's port. Optional.
 * @return {boolean} Returns `true` if the URL's port matches the specified port or if no port is provided; otherwise, `false`.
 */
export function matchPort(url: URL, port?: string): boolean {
	return !port || port === url.port;
}

/**
 * Compares the protocol of the provided URL with a specified protocol.
 *
 * @param {URL} url - The URL object whose protocol needs to be checked.
 * @param {string} [protocol] - The protocol to compare against, without the trailing colon. If not provided, the method will always return true.
 * @return {boolean} Returns `true` if the protocol matches or if no protocol is specified; otherwise, `false`.
 */
export function matchProtocol(url: URL, protocol?: string): boolean {
	return !protocol || protocol === url.protocol.slice(0, -1);
}

/**
 * Matches a given URL's hostname against a specified hostname, with optional support for wildcard patterns.
 *
 * @param {URL} url - The URL object whose hostname is to be matched.
 * @param {string} [hostname] - The hostname to match against. Supports wildcard patterns if `allowWildcard` is true.
 * @param {boolean} [allowWildcard] - Indicates whether wildcard patterns in the `hostname` parameter are allowed.
 * @return {boolean} - Returns `true` if the URL's hostname matches the given hostname criteria; otherwise, `false`.
 */

Review comment (suggestion):
Just wondering whether you already have a precedent for how you show optional parameters and suggesting that you be consistent with that that if it exists. 

In docs, for example, I think we tend to "front-load" the optional part, or even if we don't, it's usually lowercase and in parenthesis:

```
 * @param {string} [port] - (optional) The port to match against the URL's port.
```

Not suggesting you need to change this! Only a reminder that if you *do* have other optional params already written, it's nice if they're written the same way consistently!



Code after:
	pathname?: string;
	protocol?: string;
	port?: string;
};

/**
 * Evaluates whether a given URL matches the specified remote pattern based on protocol, hostname, port, and pathname.
 *
 * @param {URL} url - The URL object to be matched against the remote pattern.
 * @param {RemotePattern} remotePattern - The remote pattern object containing the protocol, hostname, port, and pathname to match.
 * @return {boolean} Returns `true` if the URL matches the given remote pattern; otherwise, `false`.
 */
export function matchPattern(url: URL, remotePattern: RemotePattern): boolean {
	return (
		matchProtocol(url, remotePattern.protocol) &&
		matchHostname(url, remotePattern.hostname, true) &&
		matchPort(url, remotePattern.port) &&
		matchPathname(url, remotePattern.pathname, true)
	);
}

/**
 * Checks if the given URL's port matches the specified port. If no port is provided, it returns `true`.
 *
 * @param {URL} url - The URL object whose port will be checked.
 * @param {string} [port=] - The port to match against the URL's port. Optional.
 * @return {boolean} Returns `true` if the URL's port matches the specified port or if no port is provided; otherwise, `false`.
 */
export function matchPort(url: URL, port?: string): boolean {
	return !port || port === url.port;
}

/**
 * Compares the protocol of the provided URL with a specified protocol.
 *
 * @param {URL} url - The URL object whose protocol needs to be checked.
 * @param {string} [protocol] - The protocol to compare against, without the trailing colon. If not provided, the method will always return `true`.
 * @return {boolean} Returns `true` if the protocol matches or if no protocol is specified; otherwise, `false`.
 */
export function matchProtocol(url: URL, protocol?: string): boolean {
	return !protocol || protocol === url.protocol.slice(0, -1);
}

/**
 * Matches a given URL's hostname against a specified hostname, with optional support for wildcard patterns.
 *
 * @param {URL} url - The URL object whose hostname is to be matched.
 * @param {string} [hostname] - The hostname to match against. Supports wildcard patterns if `allowWildcard` is `true`.
 * @param {boolean} [allowWildcard=false] - Indicates whether wildcard patterns in the `hostname` parameter are allowed.
 * @return {boolean} - Returns `true` if the URL's hostname matches the given hostname criteria; otherwise, `false`.
 */