File size: 933 Bytes
01fcadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
function search(input: string, template: string) {
    try {
        // input is a valid URL:
        // eg: https://example.com, https://example.com/test?q=param
        return new URL(input).toString();
    } catch (err) {
        // input was not a valid URL
    }

    try {
        // input is a valid URL when http:// is added to the start:
        // eg: example.com, https://example.com/test?q=param
        const url = new URL(`http://${input}`);
        // only if the hostname has a TLD/subdomain
        if (url.hostname.includes(".")) return url.toString();
    } catch (err) {
        // input was not valid URL
    }

    // input may have been a valid URL, however the hostname was invalid

    // Attempts to convert the input to a fully qualified URL have failed
    // Treat the input as a search query
    return template.replace("%s", encodeURIComponent(input));
}

export { search };