This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
jsuri-1.1.1.js
473 lines (385 loc) · 13.3 KB
/
jsuri-1.1.1.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*!
* jsUri v1.1.1
* https://github.com/derek-watson/jsUri
*
* Copyright 2011, Derek Watson
* Released under the MIT license.
* http://jquery.org/license
*
* Includes parseUri regular expressions
* http://blog.stevenlevithan.com/archives/parseuri
* Copyright 2007, Steven Levithan
* Released under the MIT license.
*
* Date: Mon Nov 14 20:06:34 2011 -0800
*/
var Query = function (queryString) {
// query string parsing, parameter manipulation and stringification
'use strict';
var // parseQuery(q) parses the uri query string and returns a multi-dimensional array of the components
parseQuery = function (q) {
var arr = [], i, ps, p, keyval;
if (typeof (q) === 'undefined' || q === null || q === '') {
return arr;
}
if (q.indexOf('?') === 0) {
q = q.substring(1);
}
ps = q.toString().split(/[&;]/);
for (i = 0; i < ps.length; i++) {
p = ps[i];
keyval = p.split('=');
arr.push([keyval[0], keyval[1]]);
}
return arr;
},
params = parseQuery(queryString),
// toString() returns a string representation of the internal state of the object
toString = function () {
var s = '', i, param;
for (i = 0; i < params.length; i++) {
param = params[i];
if (s.length > 0) {
s += '&';
}
s += param.join('=');
}
return s.length > 0 ? '?' + s : s;
},
decode = function (s) {
s = decodeURIComponent(s);
s = s.replace('+', ' ');
return s;
},
// getParamValues(key) returns the first query param value found for the key 'key'
getParamValue = function (key) {
var param, i;
for (i = 0; i < params.length; i++) {
param = params[i];
if (decode(key) === decode(param[0])) {
return param[1];
}
}
},
// getParamValues(key) returns an array of query param values for the key 'key'
getParamValues = function (key) {
var arr = [], i, param;
for (i = 0; i < params.length; i++) {
param = params[i];
if (decode(key) === decode(param[0])) {
arr.push(param[1]);
}
}
return arr;
},
// deleteParam(key) removes all instances of parameters named (key)
// deleteParam(key, val) removes all instances where the value matches (val)
deleteParam = function (key, val) {
var arr = [], i, param, keyMatchesFilter, valMatchesFilter;
for (i = 0; i < params.length; i++) {
param = params[i];
keyMatchesFilter = decode(param[0]) === decode(key);
valMatchesFilter = decode(param[1]) === decode(val);
if ((arguments.length === 1 && !keyMatchesFilter) || (arguments.length === 2 && !keyMatchesFilter && !valMatchesFilter)) {
arr.push(param);
}
}
params = arr;
return this;
},
// addParam(key, val) Adds an element to the end of the list of query parameters
// addParam(key, val, index) adds the param at the specified position (index)
addParam = function (key, val, index) {
if (arguments.length === 3 && index !== -1) {
index = Math.min(index, params.length);
params.splice(index, 0, [key, val]);
} else if (arguments.length > 0) {
params.push([key, val]);
}
return this;
},
// replaceParam(key, newVal) deletes all instances of params named (key) and replaces them with the new single value
// replaceParam(key, newVal, oldVal) deletes only instances of params named (key) with the value (val) and replaces them with the new single value
// this function attempts to preserve query param ordering
replaceParam = function (key, newVal, oldVal) {
var index = -1, i, param;
if (arguments.length === 3) {
for (i = 0; i < params.length; i++) {
param = params[i];
if (decode(param[0]) === decode(key) && decodeURIComponent(param[1]) === decode(oldVal)) {
index = i;
break;
}
}
deleteParam(key, oldVal).addParam(key, newVal, index);
} else {
for (i = 0; i < params.length; i++) {
param = params[i];
if (decode(param[0]) === decode(key)) {
index = i;
break;
}
}
deleteParam(key);
addParam(key, newVal, index);
}
return this;
};
// public api
return {
getParamValue: getParamValue,
getParamValues: getParamValues,
deleteParam: deleteParam,
addParam: addParam,
replaceParam: replaceParam,
toString: toString
};
};
var Uri = function (uriString) {
// uri string parsing, attribute manipulation and stringification
'use strict';
/*global Query: true */
/*jslint regexp: false, plusplus: false */
var strictMode = false,
// parseUri(str) parses the supplied uri and returns an object containing its components
parseUri = function (str) {
/*jslint unparam: true */
var parsers = {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
},
keys = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
q = {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
m = parsers[strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) {
uri[keys[i]] = m[i] || "";
}
uri[q.name] = {};
uri[keys[12]].replace(q.parser, function ($0, $1, $2) {
if ($1) {
uri[q.name][$1] = $2;
}
});
return uri;
},
uriParts = parseUri(uriString || ''),
queryObj = new Query(uriParts.query),
/*
Basic get/set functions for all properties
*/
protocol = function (val) {
if (typeof val !== 'undefined') {
uriParts.protocol = val;
}
return uriParts.protocol;
},
hasAuthorityPrefixUserPref = null,
// hasAuthorityPrefix: if there is no protocol, the leading // can be enabled or disabled
hasAuthorityPrefix = function (val) {
if (typeof val !== 'undefined') {
hasAuthorityPrefixUserPref = val;
}
if (hasAuthorityPrefixUserPref === null) {
return (uriParts.source.indexOf('//') !== -1);
} else {
return hasAuthorityPrefixUserPref;
}
},
userInfo = function (val) {
if (typeof val !== 'undefined') {
uriParts.userInfo = val;
}
return uriParts.userInfo;
},
host = function (val) {
if (typeof val !== 'undefined') {
uriParts.host = val;
}
return uriParts.host;
},
port = function (val) {
if (typeof val !== 'undefined') {
uriParts.port = val;
}
return uriParts.port;
},
path = function (val) {
if (typeof val !== 'undefined') {
uriParts.path = val;
}
return uriParts.path;
},
query = function (val) {
if (typeof val !== 'undefined') {
queryObj = new Query(val);
}
return queryObj;
},
anchor = function (val) {
if (typeof val !== 'undefined') {
uriParts.anchor = val;
}
return uriParts.anchor;
},
/*
Fluent setters for Uri uri properties
*/
setProtocol = function (val) {
protocol(val);
return this;
},
setHasAuthorityPrefix = function (val) {
hasAuthorityPrefix(val);
return this;
},
setUserInfo = function (val) {
userInfo(val);
return this;
},
setHost = function (val) {
host(val);
return this;
},
setPort = function (val) {
port(val);
return this;
},
setPath = function (val) {
path(val);
return this;
},
setQuery = function (val) {
query(val);
return this;
},
setAnchor = function (val) {
anchor(val);
return this;
},
/*
Query method wrappers
*/
getQueryParamValue = function (key) {
return query().getParamValue(key);
},
getQueryParamValues = function (key) {
return query().getParamValues(key);
},
deleteQueryParam = function (key, val) {
if (arguments.length === 2) {
query().deleteParam(key, val);
} else {
query().deleteParam(key);
}
return this;
},
addQueryParam = function (key, val, index) {
if (arguments.length === 3) {
query().addParam(key, val, index);
} else {
query().addParam(key, val);
}
return this;
},
replaceQueryParam = function (key, newVal, oldVal) {
if (arguments.length === 3) {
query().replaceParam(key, newVal, oldVal);
} else {
query().replaceParam(key, newVal);
}
return this;
},
/*
Serialization
*/
// toString() stringifies the current state of the uri
toString = function () {
var s = '',
is = function (s) {
return (s !== null && s !== '');
};
if (is(protocol())) {
s += protocol();
if (protocol().indexOf(':') !== protocol().length - 1) {
s += ':';
}
s += '//';
} else {
if (hasAuthorityPrefix() && is(host())) {
s += '//';
}
}
if (is(userInfo()) && is(host())) {
s += userInfo();
if (userInfo().indexOf('@') !== userInfo().length - 1) {
s += '@';
}
}
if (is(host())) {
s += host();
if (is(port())) {
s += ':' + port();
}
}
if (is(path())) {
s += path();
} else {
if (is(host()) && (is(query().toString()) || is(anchor()))) {
s += '/';
}
}
if (is(query().toString())) {
if (query().toString().indexOf('?') !== 0) {
s += '?';
}
s += query().toString();
}
if (is(anchor())) {
if (anchor().indexOf('#') !== 0) {
s += '#';
}
s += anchor();
}
return s;
},
/*
Cloning
*/
// clone() returns a new, identical Uri instance
clone = function () {
return new Uri(toString());
};
// public api
return {
protocol: protocol,
hasAuthorityPrefix: hasAuthorityPrefix,
userInfo: userInfo,
host: host,
port: port,
path: path,
query: query,
anchor: anchor,
setProtocol: setProtocol,
setHasAuthorityPrefix: setHasAuthorityPrefix,
setUserInfo: setUserInfo,
setHost: setHost,
setPort: setPort,
setPath: setPath,
setQuery: setQuery,
setAnchor: setAnchor,
getQueryParamValue: getQueryParamValue,
getQueryParamValues: getQueryParamValues,
deleteQueryParam: deleteQueryParam,
addQueryParam: addQueryParam,
replaceQueryParam: replaceQueryParam,
toString: toString,
clone: clone
};
};
/* add compatibility for users of jsUri <= 1.1.1 */
var jsUri = Uri;