-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
289 lines (260 loc) · 6.79 KB
/
index.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* External dependencies
*/
import { parse, stringify } from 'qs';
const URL_REGEXP = /^(?:https?:)?\/\/\S+$/i;
const EMAIL_REGEXP = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
/**
* Determines whether the given string looks like a URL.
*
* @param {string} url The string to scrutinise.
*
* @return {boolean} Whether or not it looks like a URL.
*/
export function isURL( url ) {
return URL_REGEXP.test( url );
}
/**
* Returns the protocol part of the URL.
*
* @param {string} url The full URL.
*
* @return {?string} The protocol part of the URL.
*/
export function getProtocol( url ) {
const matches = /^([^\s:]+:)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
/**
* Tests if a url protocol is valid.
*
* @param {string} protocol The url protocol.
*
* @return {boolean} True if the argument is a valid protocol (e.g. http:, tel:).
*/
export function isValidProtocol( protocol ) {
if ( ! protocol ) {
return false;
}
return /^[a-z\-.\+]+[0-9]*:$/i.test( protocol );
}
/**
* Returns the authority part of the URL.
*
* @param {string} url The full URL.
*
* @return {?string} The authority part of the URL.
*/
export function getAuthority( url ) {
const matches = /^[^\/\s:]+:(?:\/\/)?\/?([^\/\s#?]+)[\/#?]{0,1}\S*$/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
/**
* Checks for invalid characters within the provided authority.
*
* @param {string} authority A string containing the URL authority.
*
* @return {boolean} True if the argument contains a valid authority.
*/
export function isValidAuthority( authority ) {
if ( ! authority ) {
return false;
}
return /^[^\s#?]+$/.test( authority );
}
/**
* Returns the path part of the URL.
*
* @param {string} url The full URL.
*
* @return {?string} The path part of the URL.
*/
export function getPath( url ) {
const matches = /^[^\/\s:]+:(?:\/\/)?[^\/\s#?]+[\/]([^\s#?]+)[#?]{0,1}\S*$/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
/**
* Checks for invalid characters within the provided path.
*
* @param {string} path The URL path.
*
* @return {boolean} True if the argument contains a valid path
*/
export function isValidPath( path ) {
if ( ! path ) {
return false;
}
return /^[^\s#?]+$/.test( path );
}
/**
* Returns the query string part of the URL.
*
* @param {string} url The full URL.
*
* @return {?string} The query string part of the URL.
*/
export function getQueryString( url ) {
const matches = /^\S+?\?([^\s#]+)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
/**
* Checks for invalid characters within the provided query string.
*
* @param {string} queryString The query string.
*
* @return {boolean} True if the argument contains a valid query string.
*/
export function isValidQueryString( queryString ) {
if ( ! queryString ) {
return false;
}
return /^[^\s#?\/]+$/.test( queryString );
}
/**
* Returns the fragment part of the URL.
*
* @param {string} url The full URL
*
* @return {?string} The fragment part of the URL.
*/
export function getFragment( url ) {
const matches = /^\S+?(#[^\s\?]*)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
/**
* Checks for invalid characters within the provided fragment.
*
* @param {string} fragment The url fragment.
*
* @return {boolean} True if the argument contains a valid fragment.
*/
export function isValidFragment( fragment ) {
if ( ! fragment ) {
return false;
}
return /^#[^\s#?\/]*$/.test( fragment );
}
/**
* Appends arguments as querystring to the provided URL. If the URL already
* includes query arguments, the arguments are merged with (and take precedent
* over) the existing set.
*
* @param {?string} url URL to which arguments should be appended. If omitted,
* only the resulting querystring is returned.
* @param {Object} args Query arguments to apply to URL.
*
* @return {string} URL with arguments applied.
*/
export function addQueryArgs( url = '', args ) {
// If no arguments are to be appended, return original URL.
if ( ! args || ! Object.keys( args ).length ) {
return url;
}
let baseUrl = url;
// Determine whether URL already had query arguments.
const queryStringIndex = url.indexOf( '?' );
if ( queryStringIndex !== -1 ) {
// Merge into existing query arguments.
args = Object.assign(
parse( url.substr( queryStringIndex + 1 ) ),
args
);
// Change working base URL to omit previous query arguments.
baseUrl = baseUrl.substr( 0, queryStringIndex );
}
return baseUrl + '?' + stringify( args );
}
/**
* Returns a single query argument of the url
*
* @param {string} url URL
* @param {string} arg Query arg name
*
* @return {Array|string} Query arg value.
*/
export function getQueryArg( url, arg ) {
const queryStringIndex = url.indexOf( '?' );
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {};
return query[ arg ];
}
/**
* Determines whether the URL contains a given query arg.
*
* @param {string} url URL
* @param {string} arg Query arg name
*
* @return {boolean} Whether or not the URL contains the query aeg.
*/
export function hasQueryArg( url, arg ) {
return getQueryArg( url, arg ) !== undefined;
}
/**
* Removes arguments from the query string of the url
*
* @param {string} url URL
* @param {...string} args Query Args
*
* @return {string} Updated URL
*/
export function removeQueryArgs( url, ...args ) {
const queryStringIndex = url.indexOf( '?' );
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {};
const baseUrl = queryStringIndex !== -1 ? url.substr( 0, queryStringIndex ) : url;
args.forEach( ( arg ) => delete query[ arg ] );
return baseUrl + '?' + stringify( query );
}
/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
*
* @param {string} url The URL to test
*
* @return {string} The updated URL
*/
export function prependHTTP( url ) {
if ( ! USABLE_HREF_REGEXP.test( url ) && ! EMAIL_REGEXP.test( url ) ) {
return 'http://' + url;
}
return url;
}
/**
* Safely decodes a URI with `decodeURI`. Returns the URI unmodified if
* `decodeURI` throws an error.
*
* @param {string} uri URI to decode.
*
* @return {string} Decoded URI if possible.
*/
export function safeDecodeURI( uri ) {
try {
return decodeURI( uri );
} catch ( uriError ) {
return uri;
}
}
/**
* Returns a URL for display.
*
* @param {string} url Original URL.
*
* @return {string} Displayed URL.
*/
export function filterURLForDisplay( url ) {
// Remove protocol and www prefixes.
const filteredURL = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );
// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
return filteredURL.replace( '/', '' );
}
return filteredURL;
}