| 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 | 1×
1×
1×
1×
1×
1×
1×
1×
5×
318×
530×
1×
1×
103×
103×
1030×
1030×
618×
1×
198×
198×
97×
101×
5×
1×
5×
5×
5×
5×
5×
5×
5×
5×
16×
16×
16×
16×
98×
98×
24×
24×
24×
24×
14×
24×
2×
2×
1×
1×
1×
1×
1×
33×
106×
18×
2×
2×
1×
1×
1×
1×
1×
25×
10×
23×
9×
1×
| const d = require("describe-property");
const mergeQuery = require("./utils/mergeQuery");
const stringifyQuery = require("./utils/stringifyQuery");
const parseQuery = require("./utils/parseQuery");
const parseURL = require("./utils/parseURL");
const R = require("ramda");
/**
* Standard ports for HTTP protocols.
*/
const STANDARD_PORTS = {
"http:": "80",
"https:": "443"
};
function propertyAlias(propertyName, defaultValue) {
return d.gs(function () {
return this.properties[propertyName] || (R.isNil(defaultValue) ? null : defaultValue);
}, function (value) {
this.properties[propertyName] = value;
});
}
// Order is important here. Later properties take priority.
const PROPERTY_NAMES = [
"protocol",
"auth",
"hostname",
"port",
"host",
"pathname",
"search",
"queryString",
"query",
"path"
];
function setProperties(location, properties) {
let propertyName;
for (let i = 0, len = PROPERTY_NAMES.length; i < len; ++i) {
propertyName = PROPERTY_NAMES[i];
if (properties.hasOwnProperty(propertyName) && propertyName in location) {
location[propertyName] = properties[propertyName];
}
}
}
/**
* A URL location, analogous to window.location.
*
* Options may be any of the following:
*
* - protocol
* - auth
* - hostname
* - port
* - host (overrides hostname and port)
* - pathname
* - search
* - queryString (overrides search)
* - query (overrides queryString/search)
* - path (overrides pathname and query/queryString/search)
*
* Alternatively, options may be a URL string.
*/
function Location(options) {
this.properties = {};
if (typeof options === "string") {
this.href = options;
} else if (options) {
setProperties(this, options);
}
}
Object.defineProperties(Location.prototype, {
/**
* Creates and returns a new Location with the path and query of
* the given location appended.
*/
concat: d(function (location) {
Eif (!R.is(Location, location)) {
location = new Location(location);
}
let pathname = this.pathname;
const extraPathname = location.pathname;
Eif (extraPathname !== "/") {
pathname = pathname.replace(/\/*$/, "/") + extraPathname.replace(/^\/*/, "");
}
const query = mergeQuery(this.query, location.query);
return new Location({
protocol: location.protocol || this.protocol,
auth: location.auth || this.auth,
hostname: location.hostname || this.hostname,
port: location.port || this.port,
pathname,
query
});
}),
/**
* The full URL.
*/
href: d.gs(function () {
const auth = this.auth;
const host = this.host;
const path = this.path;
return host ? `${this.protocol}//${(auth ? `${auth}@` : "") + host + path}` : path;
}, function (value) {
const parsed = parseURL(value);
setProperties(this, {
protocol: parsed.protocol,
auth: parsed.auth,
hostname: parsed.hostname,
port: parsed.port,
pathname: parsed.pathname,
search: parsed.search
});
}),
/**
* The portion of the URL that denotes the protocol, including the
* trailing colon (e.g. "http:" or "https:").
*/
protocol: propertyAlias("protocol"),
/**
* The username:password used in the URL, if any.
*/
auth: propertyAlias("auth", ""),
/**
* The full name of the host, including the port number when using
* a non-standard port.
*/
host: d.gs(function () {
const protocol = this.protocol;
let host = this.hostname;
const port = this.port;
if (!R.isNil(port) && port !== STANDARD_PORTS[protocol]) {
host += `:${port}`;
}
return host;
}, function (value) {
let index;
if (R.is(String, value) && R.contains(":", value)) {
index = R.findIndex(R.equals(":"), value);
this.hostname = value.substring(0, index);
this.port = value.substring(index + 1);
} else {
this.hostname = value;
this.port = null;
}
}),
/**
* The name of the host without the port.
*/
hostname: propertyAlias("hostname"),
/**
* The port number as a string.
*/
port: d.gs(function () {
return this.properties.port || (this.protocol ? STANDARD_PORTS[this.protocol] : null);
}, function (value) {
this.properties.port = value ? String(value) : null;
}),
/**
* The URL path without the query string.
*/
pathname: propertyAlias("pathname", "/"),
/**
* The URL path with query string.
*/
path: d.gs(function () {
return this.pathname + this.search;
}, function (value) {
let index;
if (typeof value === "string" && R.contains("?", value)) {
index = R.findIndex(R.equals("?"), value);
this.pathname = value.substring(0, index);
this.search = value.substring(index);
} else {
this.pathname = value;
this.search = null;
}
}),
/**
* The query string, including the preceeding ?.
*/
search: propertyAlias("search", ""),
/**
* The query string of the URL, without the preceeding ?.
*/
queryString: d.gs(function () {
return this.search.substring(1);
}, function (value) {
this.search = value && `?${value}`;
}),
/**
* An object of data in the query string.
*/
query: d.gs(function () {
return parseQuery(this.queryString);
}, function (value) {
this.queryString = stringifyQuery(value);
}),
toJSON: d(function () {
return this.href;
}),
toString: d(function () {
return this.href;
})
});
module.exports = Location;
|