"use strict";1/**2*3* @param {string} input4* @param {string} template Template for a search query.5* @returns {string} Fully qualified URL6*/7function search(input, template) {8try {9// input is a valid URL:10// eg: https://example.com, https://example.com/test?q=param11return new URL(input).toString();12} catch (err) {13// input was not a valid URL14}1516try {17// input is a valid URL when http:// is added to the start:18// eg: example.com, https://example.com/test?q=param19const url = new URL(`http://${input}`);20// only if the hostname has a TLD/subdomain21if (url.hostname.includes(".")) return url.toString();22} catch (err) {23// input was not valid URL24}2526// input may have been a valid URL, however the hostname was invalid2728// Attempts to convert the input to a fully qualified URL have failed29// Treat the input as a search query30return template.replace("%s", encodeURIComponent(input));31}323334