index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. var hrefInfo = require("./hrefInfo");
  3. var parseHost = require("./host");
  4. var parsePath = require("./path");
  5. var parsePort = require("./port");
  6. var parseQuery = require("./query");
  7. var parseUrlString = require("./urlstring");
  8. var pathUtils = require("../util/path");
  9. function parseFromUrl(url, options, fallback)
  10. {
  11. if (url)
  12. {
  13. var urlObj = parseUrl(url, options);
  14. // Because the following occurs in the relate stage for "to" URLs,
  15. // such had to be mostly duplicated here
  16. var pathArray = pathUtils.resolveDotSegments(urlObj.path.absolute.array);
  17. urlObj.path.absolute.array = pathArray;
  18. urlObj.path.absolute.string = "/" + pathUtils.join(pathArray);
  19. return urlObj;
  20. }
  21. else
  22. {
  23. return fallback;
  24. }
  25. }
  26. function parseUrl(url, options)
  27. {
  28. var urlObj = parseUrlString(url, options);
  29. if (urlObj.valid===false) return urlObj;
  30. parseHost(urlObj, options);
  31. parsePort(urlObj, options);
  32. parsePath(urlObj, options);
  33. parseQuery(urlObj, options);
  34. hrefInfo(urlObj);
  35. return urlObj;
  36. }
  37. module.exports =
  38. {
  39. from: parseFromUrl,
  40. to: parseUrl
  41. };