forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.ts
201 lines (172 loc) · 4.82 KB
/
url.ts
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { JSONObject } from '@phosphor/coreutils';
import urlparse from 'url-parse';
/**
* The namespace for URL-related functions.
*/
export namespace URLExt {
/**
* Parse a url into a URL object.
*
* @param urlString - The URL string to parse.
*
* @returns A URL object.
*/
export function parse(url: string): IUrl {
if (typeof document !== 'undefined' && document) {
let a = document.createElement('a');
a.href = url;
return a;
}
return urlparse(url);
}
/**
* Normalize a url.
*/
export function normalize(url: string): string {
return url && parse(url).toString();
}
/**
* Join a sequence of url components and normalizes as in node `path.join`.
*
* @param parts - The url components.
*
* @returns the joined url.
*/
export function join(...parts: string[]): string {
parts = parts || [];
// Isolate the top element.
const top = parts[0] || '';
// Check whether protocol shorthand is being used.
const shorthand = top.indexOf('//') === 0;
// Parse the top element into a header collection.
const header = top.match(/(\w+)(:)(\/\/)?/);
const protocol = header && header[1];
const colon = protocol && header[2];
const slashes = colon && header[3];
// Construct the URL prefix.
const prefix = shorthand
? '//'
: [protocol, colon, slashes].filter(str => str).join('');
// Construct the URL body omitting the prefix of the top value.
const body = [top.indexOf(prefix) === 0 ? top.replace(prefix, '') : top]
// Filter out top value if empty.
.filter(str => str)
// Remove leading slashes in all subsequent URL body elements.
.concat(parts.slice(1).map(str => str.replace(/^\//, '')))
.join('/')
// Replace multiple slashes with one.
.replace(/\/+/g, '/');
return prefix + body;
}
/**
* Encode the components of a multi-segment url.
*
* @param url - The url to encode.
*
* @returns the encoded url.
*
* #### Notes
* Preserves the `'/'` separators.
* Should not include the base url, since all parts are escaped.
*/
export function encodeParts(url: string): string {
return join(...url.split('/').map(encodeURIComponent));
}
/**
* Return a serialized object string suitable for a query.
*
* @param object - The source object.
*
* @returns an encoded url query.
*
* #### Notes
* Modified version of [stackoverflow](http://stackoverflow.com/a/30707423).
*/
export function objectToQueryString(value: JSONObject): string {
const keys = Object.keys(value).filter(key => key.length > 0);
if (!keys.length) {
return '';
}
return (
'?' +
keys
.map(key => {
const content = encodeURIComponent(String(value[key]));
return key + (content ? '=' + content : '');
})
.join('&')
);
}
/**
* Return a parsed object that represents the values in a query string.
*/
export function queryStringToObject(
value: string
): { [key: string]: string } {
return value
.replace(/^\?/, '')
.split('&')
.reduce((acc, val) => {
const [key, value] = val.split('=');
if (key.length > 0) {
acc[key] = decodeURIComponent(value || '');
}
return acc;
}, {} as { [key: string]: string });
}
/**
* Test whether the url is a local url.
*
* #### Notes
* This function returns `false` for any fully qualified url, including
* `data:`, `file:`, and `//` protocol URLs.
*/
export function isLocal(url: string): boolean {
const { protocol } = parse(url);
return url.toLowerCase().indexOf(protocol) !== 0 && url.indexOf('/') !== 0;
}
/**
* The interface for a URL object
*/
export interface IUrl {
/**
* The full URL string that was parsed with both the protocol and host
* components converted to lower-case.
*/
href?: string;
/**
* Identifies the URL's lower-cased protocol scheme.
*/
protocol?: string;
/**
* The full lower-cased host portion of the URL, including the port if
* specified.
*/
host?: string;
/**
* The lower-cased host name portion of the host component without the
* port included.
*/
hostname?: string;
/**
* The numeric port portion of the host component.
*/
port?: string;
/**
* The entire path section of the URL.
*/
pathname?: string;
/**
* The "fragment" portion of the URL including the leading ASCII hash
* `(#)` character
*/
hash?: string;
/**
* The search element, including leading question mark (`'?'`), if any,
* of the URL.
*/
search?: string;
}
}