-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (31 loc) · 855 Bytes
/
index.js
File metadata and controls
33 lines (31 loc) · 855 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
function encode (o, sep) {
var list = [];
var key;
for (key in o) {
if (o[key] != null && typeof o[key] != 'object' &&
typeof o[key] != 'function') {
list.push(encodeURIComponent(key) + '=' + encodeURIComponent(o[key]));
}
}
return list.join(sep || '&');
}
var REXP_SPLIT = /&|&|;/gmi;
function decode (str, sep) {
sep = sep||REXP_SPLIT;
var result = {};
var expr = str.split(sep);
var key, val, index;
for (var i = 0, len = expr.length; i < len; i++) {
index = expr[i].indexOf('=');
key = expr[i].substring(0, index);
val = expr[i].substring(index+1);
if (val) {
result[decodeURIComponent(key)] = decodeURIComponent(val);
}
}
return result;
};
module.exports = {
encode: encode,
decode: decode
};