index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. const hasToStringTag = require("has-to-string-tag-x");
  3. const isObject = require("is-object");
  4. const toString = Object.prototype.toString;
  5. const urlClass = "[object URL]";
  6. const hash = "hash";
  7. const host = "host";
  8. const hostname = "hostname";
  9. const href = "href";
  10. const password = "password";
  11. const pathname = "pathname";
  12. const port = "port";
  13. const protocol = "protocol";
  14. const search = "search";
  15. const username = "username";
  16. const isURL = (url, supportIncomplete/*=false*/) =>
  17. {
  18. if (!isObject(url)) return false;
  19. // Native implementation in older browsers
  20. if (!hasToStringTag && toString.call(url) === urlClass) return true;
  21. if (!(href in url)) return false;
  22. if (!(protocol in url)) return false;
  23. if (!(username in url)) return false;
  24. if (!(password in url)) return false;
  25. if (!(hostname in url)) return false;
  26. if (!(port in url)) return false;
  27. if (!(host in url)) return false;
  28. if (!(pathname in url)) return false;
  29. if (!(search in url)) return false;
  30. if (!(hash in url)) return false;
  31. if (supportIncomplete !== true)
  32. {
  33. if (!isObject(url.searchParams)) return false;
  34. // TODO :: write a separate isURLSearchParams ?
  35. }
  36. return true;
  37. }
  38. isURL.lenient = url =>
  39. {
  40. return isURL(url, true);
  41. };
  42. module.exports = isURL;