| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | "use strict"; |
| 2 | const hasToStringTag = require("has-to-string-tag-x"); |
| 3 | const isObject = require("is-object"); |
| 4 | |
| 5 | const toString = Object.prototype.toString; |
| 6 | const urlClass = "[object URL]"; |
| 7 | |
| 8 | const hash = "hash"; |
| 9 | const host = "host"; |
| 10 | const hostname = "hostname"; |
| 11 | const href = "href"; |
| 12 | const password = "password"; |
| 13 | const pathname = "pathname"; |
| 14 | const port = "port"; |
| 15 | const protocol = "protocol"; |
| 16 | const search = "search"; |
| 17 | const username = "username"; |
| 18 | |
| 19 | |
| 20 | |
| 21 | const 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 | |
| 51 | isURL.lenient = url => |
| 52 | { |
| 53 | return isURL(url, true); |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | |
| 58 | module.exports = isURL; |