-
Notifications
You must be signed in to change notification settings - Fork 6
/
url2.js
151 lines (132 loc) · 4.29 KB
/
url2.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var URL = require("url");
// Node’s URL parse produces:
// http://user:password@foo.com:8080/path/to/foo/bar?a=10&c=20&d=30#hash
// ^ ^^^^- auth ----^ ^-----^ |^- pathname----^^- search-----^^ ^
// | ||| hostname | ^- query ----^| |
// protocol | ^--^^- path ----------------------^| |
// | || | port hash^
// | slashes ^- host ---^ |
// ^- href ------------------------------------------------------------^
// Node's URL format uses:
// protocol: gets a colon added if it doesn't have one yet
// slashes: used
// auth: used
// user: not used
// password: not used
// host:
// hostname: used if there's no host
// port: used if there's no host
// path: NOT USED AT ALL EVER
// pathname: used
// root: not used, not produced
// directory: not used, not produced
// directories: not used, not produced
// file: not used, not produced
// search: used
// query: used if object and no search
// hash: used
exports.resolve = URL.resolve;
exports.resolveObject = URL.resolveObject;
exports.parse = parse;
function parse() {
var object = URL.parse.apply(URL, arguments);
object.pathname = object.pathname || "";
object.root = !!object.pathname.length && object.pathname[0] === "/";
if (object.root) {
object.relative = object.pathname.slice(1);
} else {
object.relative = object.pathname;
}
if (object.relative.length) {
object.directories = object.relative.split("/");
object.file = object.directories.pop();
} else {
object.directories = [];
object.file = null;
}
return object;
}
exports.format = format;
function format(object) {
if ("file" in object) {
object.directories.push(object.file);
delete object.file;
}
if ("directories" in object) {
object.relative = object.directories.join("/");
delete object.directories;
}
if ("relative" in object) {
if (object.root) {
object.pathname = "/" + object.relative;
} else {
object.pathname = object.relative;
}
delete object.relative;
}
if (object.path !== null && object.path !== void 0) {
var index = object.path.indexOf("?");
if (index == -1) {
object.pathname = object.path;
object.search = "";
} else {
object.pathname = object.path.slice(0, index);
object.search = object.path.slice(index);
}
}
return URL.format(object);
}
exports.relativeObject = relativeObject;
function relativeObject(source, target) {
source = parse(source);
target = parse(target);
delete target.href;
if (
target.protocol === source.protocol &&
target.slashes === source.slashes &&
target.auth === source.auth &&
target.host === source.host
) {
delete target.protocol;
delete target.slashes;
delete target.auth;
delete target.hostname;
delete target.port;
delete target.host;
if (Boolean(target.root) === Boolean(source.root)) {
delete target.path;
delete target.root;
while (
source.directories.length &&
target.directories.length &&
target.directories[0] == source.directories[0]
) {
target.directories.shift();
source.directories.shift();
}
while (source.directories.length) {
source.directories.shift();
target.directories.unshift('..');
}
if (
!target.root &&
!target.directories.length &&
!target.file && source.file
) {
target.directories.push('.');
}
if (
target.directories.length === 0 &&
target.file === null &&
source.search
) {
target.search = source.search;
}
}
}
return target;
}
exports.relative = relative;
function relative(source, target) {
return format(relativeObject(source, target));
}