blob: 70ed1d7ae3efe359273f4bae26c1d6c06b50a649 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001"use strict";
2const hasToStringTag = require("has-to-string-tag-x");
3const isObject = require("is-object");
4
5const toString = Object.prototype.toString;
6const urlClass = "[object URL]";
7
8const hash = "hash";
9const host = "host";
10const hostname = "hostname";
11const href = "href";
12const password = "password";
13const pathname = "pathname";
14const port = "port";
15const protocol = "protocol";
16const search = "search";
17const username = "username";
18
19
20
21const isURL = (url, supportIncomplete/*=false*/) =>
22{
23 if (!isObject(url)) return false;
24
25 // Native implementation in older browsers
26 if (!hasToStringTag && toString.call(url) === urlClass) return true;
27
28 if (!(href in url)) return false;
29 if (!(protocol in url)) return false;
30 if (!(username in url)) return false;
31 if (!(password in url)) return false;
32 if (!(hostname in url)) return false;
33 if (!(port in url)) return false;
34 if (!(host in url)) return false;
35 if (!(pathname in url)) return false;
36 if (!(search in url)) return false;
37 if (!(hash in url)) return false;
38
39 if (supportIncomplete !== true)
40 {
41 if (!isObject(url.searchParams)) return false;
42
43 // TODO :: write a separate isURLSearchParams ?
44 }
45
46 return true;
47}
48
49
50
51isURL.lenient = url =>
52{
53 return isURL(url, true);
54};
55
56
57
58module.exports = isURL;