forked from boo1ean/shitty-qs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (40 loc) · 825 Bytes
/
index.js
File metadata and controls
49 lines (40 loc) · 825 Bytes
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
var parseShittyQueryString = function(q) {
// Check if given piece of shit is a string
if (typeof q !== 'string') {
return {};
}
// Remove fucking question mark
if (q[0] === '?') {
q = q.slice(1);
}
// TRIM TRIM
q = q.trim();
// IS IT EMPTY?
if (!q.length) {
return {};
}
return q.replace(/\+/g, ' ').split('#').reduce(function(r, p) {
var kv = p.split('=');
var key = kv[0];
var val = kv[1];
try {
key = decodeURIComponent(key);
} catch (WTF) {
// wtf Johny?
}
try {
val = val === undefined ? null : decodeURIComponent(val);
} catch (WTF) {
// WTF?
}
if (!r.hasOwnProperty(key)) {
r[key] = val;
} else if (Array.isArray(r[key])) {
r[key].push(val);
} else {
r[key] = [r[key], val];
}
return r;
}, {});
};
module.exports = parseShittyQueryString;