File size: 602 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { normalizeWhoisField } from './normalize-whois-entry';

// Normalize a WHOIS URL.
export function normalizeWhoisURL( url: string | string[] | undefined ): string {
	let normalisedURL = normalizeWhoisField( url );

	if ( normalisedURL === '' ) {
		return normalisedURL;
	}

	// Add a protocol if one is missing (assume HTTPS).
	if ( ! /^(http|https):\/\//.test( normalisedURL ) ) {
		normalisedURL = `https://${ normalisedURL }`;
	}

	try {
		// Throw an error if the URL is invalid.
		const urlObject = new URL( normalisedURL );

		return urlObject.href;
	} catch ( error ) {
		return '';
	}
}