You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.4 KiB

4 years ago
  1. "use strict";
  2. var constants = require("./constants");
  3. function formatAuth(urlObj, options)
  4. {
  5. if (urlObj.auth && !options.removeAuth && (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE))
  6. {
  7. return urlObj.auth + "@";
  8. }
  9. return "";
  10. }
  11. function formatHash(urlObj, options)
  12. {
  13. return urlObj.hash ? urlObj.hash : "";
  14. }
  15. function formatHost(urlObj, options)
  16. {
  17. if (urlObj.host.full && (urlObj.extra.relation.maximumAuth || options.output===constants.ABSOLUTE))
  18. {
  19. return urlObj.host.full;
  20. }
  21. return "";
  22. }
  23. function formatPath(urlObj, options)
  24. {
  25. var str = "";
  26. var absolutePath = urlObj.path.absolute.string;
  27. var relativePath = urlObj.path.relative.string;
  28. var resource = showResource(urlObj, options);
  29. if (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE || options.output===constants.ROOT_RELATIVE)
  30. {
  31. str = absolutePath;
  32. }
  33. else if (relativePath.length<=absolutePath.length && options.output===constants.SHORTEST || options.output===constants.PATH_RELATIVE)
  34. {
  35. str = relativePath;
  36. if (str === "")
  37. {
  38. var query = showQuery(urlObj,options) && !!getQuery(urlObj,options);
  39. if (urlObj.extra.relation.maximumPath && !resource)
  40. {
  41. str = "./";
  42. }
  43. else if (urlObj.extra.relation.overridesQuery && !resource && !query)
  44. {
  45. str = "./";
  46. }
  47. }
  48. }
  49. else
  50. {
  51. str = absolutePath;
  52. }
  53. if ( str==="/" && !resource && options.removeRootTrailingSlash && (!urlObj.extra.relation.minimumPort || options.output===constants.ABSOLUTE) )
  54. {
  55. str = "";
  56. }
  57. return str;
  58. }
  59. function formatPort(urlObj, options)
  60. {
  61. if (urlObj.port && !urlObj.extra.portIsDefault && urlObj.extra.relation.maximumHost)
  62. {
  63. return ":" + urlObj.port;
  64. }
  65. return "";
  66. }
  67. function formatQuery(urlObj, options)
  68. {
  69. return showQuery(urlObj,options) ? getQuery(urlObj, options) : "";
  70. }
  71. function formatResource(urlObj, options)
  72. {
  73. return showResource(urlObj,options) ? urlObj.resource : "";
  74. }
  75. function formatScheme(urlObj, options)
  76. {
  77. var str = "";
  78. if (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE)
  79. {
  80. if (!urlObj.extra.relation.minimumScheme || !options.schemeRelative || options.output===constants.ABSOLUTE)
  81. {
  82. str += urlObj.scheme + "://";
  83. }
  84. else
  85. {
  86. str += "//";
  87. }
  88. }
  89. return str;
  90. }
  91. function formatUrl(urlObj, options)
  92. {
  93. var url = "";
  94. url += formatScheme(urlObj, options);
  95. url += formatAuth(urlObj, options);
  96. url += formatHost(urlObj, options);
  97. url += formatPort(urlObj, options);
  98. url += formatPath(urlObj, options);
  99. url += formatResource(urlObj, options);
  100. url += formatQuery(urlObj, options);
  101. url += formatHash(urlObj, options);
  102. return url;
  103. }
  104. function getQuery(urlObj, options)
  105. {
  106. var stripQuery = options.removeEmptyQueries && urlObj.extra.relation.minimumPort;
  107. return urlObj.query.string[ stripQuery ? "stripped" : "full" ];
  108. }
  109. function showQuery(urlObj, options)
  110. {
  111. return !urlObj.extra.relation.minimumQuery || options.output===constants.ABSOLUTE || options.output===constants.ROOT_RELATIVE;
  112. }
  113. function showResource(urlObj, options)
  114. {
  115. var removeIndex = options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex;
  116. var removeMatchingResource = urlObj.extra.relation.minimumResource && options.output!==constants.ABSOLUTE && options.output!==constants.ROOT_RELATIVE;
  117. return !!urlObj.resource && !removeMatchingResource && !removeIndex;
  118. }
  119. module.exports = formatUrl;