Method description:
Convert string to object. To put it bluntly, it actually converts the parameter string on the url into an array object. (You’ll know just by looking at the example)
Grammar:
Receive parameters:
str The string to be converted
sep Set the delimiter, the default is ‘&’
eq Set the assignment character, the default is ‘=’
[options] maxKeys The maximum length of acceptable strings, the default is 1000
Example:
Source code:
// Parse a key=val string.
QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var obj = {};
if (!util.isString(qs) || qs.length === 0) {
Return obj;
}
var regexp = / /g;
qs = qs.split(sep);
var maxKeys = 1000;
if (options && util.isNumber(options.maxKeys)) {
maxKeys = options.maxKeys;
}
var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0 && len > maxKeys) {
len = maxKeys;
}
for (var i = 0; i < len; i) {
var x = qs[i].replace(regexp, ' '),
idx = x.indexOf(eq),
kstr, vstr, k, v;
If (idx >= 0) {
kstr = x.substr(0, idx);
vstr = x.substr(idx 1);
} else {
kstr = x;
vstr = '';
}
Try {
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
} catch (e) {
k = QueryString.unescape(kstr, true);
v = QueryString.unescape(vstr, true);
}
If (!hasOwnProperty(obj, k)) {
obj[k] = v;
} else if (util.isArray(obj[k])) {
obj[k].push(v);
} else {
obj[k] = [obj[k], v];
}
}
Return obj;
};