This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (52 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
55 lines (52 loc) · 1.3 KB
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
(function (root, factory) {
/* istanbul ignore next*/
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.anyQs = factory();
}
}(this, function () {
var defaultOptions = {
parseNumber: true
};
/**
* return an object contains all query parameters or empty object
* @param {string} url
* @return {object}
*/
function anyQs(url, options) {
var postOptions = defaultOptions;
if (typeof options === 'undefined') {
options = defaultOptions;
} else {
for (var i in options) {
postOptions[i] = options[i];
}
}
var params = {},
tempArr = decodeURIComponent(url)
.replace(/\+/g, ' ')
.match(/\w+=[^&#?\/,;]+/g);
if (!tempArr) {
return {};
}
tempArr.forEach(function (item) {
var ps = item.split('=');
if (postOptions.parseNumber) {
params[ps[0]] = /^\d+(\.\d+)?$/.test(ps[1]) ? parseFloat(ps[1]) : ps[1];
} else {
params[ps[0]] = ps[1];
}
});
return params;
}
/**
* Alternative version that don't parse number
*/
anyQs.stringOnly = function(url) {
return anyQs(url, { parseNumber: false });
}
return anyQs;
}));