1 | "use strict";
|
---|
2 |
|
---|
3 | var constants = require("./constants");
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 | function formatAuth(urlObj, options)
|
---|
8 | {
|
---|
9 | if (urlObj.auth && !options.removeAuth && (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE))
|
---|
10 | {
|
---|
11 | return urlObj.auth + "@";
|
---|
12 | }
|
---|
13 |
|
---|
14 | return "";
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | function formatHash(urlObj, options)
|
---|
20 | {
|
---|
21 | return urlObj.hash ? urlObj.hash : "";
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | function formatHost(urlObj, options)
|
---|
27 | {
|
---|
28 | if (urlObj.host.full && (urlObj.extra.relation.maximumAuth || options.output===constants.ABSOLUTE))
|
---|
29 | {
|
---|
30 | return urlObj.host.full;
|
---|
31 | }
|
---|
32 |
|
---|
33 | return "";
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | function formatPath(urlObj, options)
|
---|
39 | {
|
---|
40 | var str = "";
|
---|
41 |
|
---|
42 | var absolutePath = urlObj.path.absolute.string;
|
---|
43 | var relativePath = urlObj.path.relative.string;
|
---|
44 | var resource = showResource(urlObj, options);
|
---|
45 |
|
---|
46 | if (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE || options.output===constants.ROOT_RELATIVE)
|
---|
47 | {
|
---|
48 | str = absolutePath;
|
---|
49 | }
|
---|
50 | else if (relativePath.length<=absolutePath.length && options.output===constants.SHORTEST || options.output===constants.PATH_RELATIVE)
|
---|
51 | {
|
---|
52 | str = relativePath;
|
---|
53 |
|
---|
54 | if (str === "")
|
---|
55 | {
|
---|
56 | var query = showQuery(urlObj,options) && !!getQuery(urlObj,options);
|
---|
57 |
|
---|
58 | if (urlObj.extra.relation.maximumPath && !resource)
|
---|
59 | {
|
---|
60 | str = "./";
|
---|
61 | }
|
---|
62 | else if (urlObj.extra.relation.overridesQuery && !resource && !query)
|
---|
63 | {
|
---|
64 | str = "./";
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | str = absolutePath;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if ( str==="/" && !resource && options.removeRootTrailingSlash && (!urlObj.extra.relation.minimumPort || options.output===constants.ABSOLUTE) )
|
---|
74 | {
|
---|
75 | str = "";
|
---|
76 | }
|
---|
77 |
|
---|
78 | return str;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 |
|
---|
83 | function formatPort(urlObj, options)
|
---|
84 | {
|
---|
85 | if (urlObj.port && !urlObj.extra.portIsDefault && urlObj.extra.relation.maximumHost)
|
---|
86 | {
|
---|
87 | return ":" + urlObj.port;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return "";
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 | function formatQuery(urlObj, options)
|
---|
96 | {
|
---|
97 | return showQuery(urlObj,options) ? getQuery(urlObj, options) : "";
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | function formatResource(urlObj, options)
|
---|
103 | {
|
---|
104 | return showResource(urlObj,options) ? urlObj.resource : "";
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 | function formatScheme(urlObj, options)
|
---|
110 | {
|
---|
111 | var str = "";
|
---|
112 |
|
---|
113 | if (urlObj.extra.relation.maximumHost || options.output===constants.ABSOLUTE)
|
---|
114 | {
|
---|
115 | if (!urlObj.extra.relation.minimumScheme || !options.schemeRelative || options.output===constants.ABSOLUTE)
|
---|
116 | {
|
---|
117 | str += urlObj.scheme + "://";
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | str += "//";
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return str;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | function formatUrl(urlObj, options)
|
---|
131 | {
|
---|
132 | var url = "";
|
---|
133 |
|
---|
134 | url += formatScheme(urlObj, options);
|
---|
135 | url += formatAuth(urlObj, options);
|
---|
136 | url += formatHost(urlObj, options);
|
---|
137 | url += formatPort(urlObj, options);
|
---|
138 | url += formatPath(urlObj, options);
|
---|
139 | url += formatResource(urlObj, options);
|
---|
140 | url += formatQuery(urlObj, options);
|
---|
141 | url += formatHash(urlObj, options);
|
---|
142 |
|
---|
143 | return url;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | function getQuery(urlObj, options)
|
---|
149 | {
|
---|
150 | var stripQuery = options.removeEmptyQueries && urlObj.extra.relation.minimumPort;
|
---|
151 |
|
---|
152 | return urlObj.query.string[ stripQuery ? "stripped" : "full" ];
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 | function showQuery(urlObj, options)
|
---|
158 | {
|
---|
159 | return !urlObj.extra.relation.minimumQuery || options.output===constants.ABSOLUTE || options.output===constants.ROOT_RELATIVE;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | function showResource(urlObj, options)
|
---|
165 | {
|
---|
166 | var removeIndex = options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex;
|
---|
167 | var removeMatchingResource = urlObj.extra.relation.minimumResource && options.output!==constants.ABSOLUTE && options.output!==constants.ROOT_RELATIVE;
|
---|
168 |
|
---|
169 | return !!urlObj.resource && !removeMatchingResource && !removeIndex;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 | module.exports = formatUrl;
|
---|