normalizeUrl.js
1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
var isString = require("is-string");
var urllib = require("url");
var formatPath = require("./formatPath");
var formatPathname = require("./formatPathname");
var normalizeDirs = require("./normalizeDirs");
var parseUrl = require("./parseUrl");
// TODO :: document options
function normalizeUrl(url, options)
{
var portSuffix;
url = parseUrl(url, options);
// Remove default port
if (url.extra.portIsDefault===true && url.port!==null)
{
if (isString(url.host) === true)
{
portSuffix = ":" + url.port;
// Remove ":123" suffix from host
if (url.host.substr(-portSuffix.length) === portSuffix)
{
url.host = url.host.slice(0, -portSuffix.length);
}
}
url.port = null;
}
url.extra.directory = normalizeDirs(url.extra.directory, url.extra.directoryLeadingSlash).dir;
url.pathname = formatPathname(url);
// Remove empty query
if (url.search === "?")
{
url.search = null;
if (url.query==="?" || url.query==="") url.query = null;
}
// TODO :: remove empty hashes ("path#") ?
url.path = formatPath(url);
url.href = urllib.format(url);
return url;
}
module.exports = normalizeUrl;