Browse Source

querystring: improved speed and code cleanup

v0.9.1-release
Felix Böhm 13 years ago
committed by isaacs
parent
commit
3a5798b097
  1. 56
      lib/querystring.js

56
lib/querystring.js

@ -134,42 +134,34 @@ var stringifyPrimitive = function(v) {
QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) { QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
sep = sep || '&'; sep = sep || '&';
eq = eq || '='; eq = eq || '=';
obj = (obj === null) ? undefined : obj; if (obj === null) {
obj = undefined;
}
switch (typeof obj) { if (typeof obj === 'object') {
case 'object':
return Object.keys(obj).map(function(k) { return Object.keys(obj).map(function(k) {
var ks = QueryString.escape(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) { if (Array.isArray(obj[k])) {
return obj[k].map(function(v) { return obj[k].map(function(v) {
return QueryString.escape(stringifyPrimitive(k)) + return ks + QueryString.escape(stringifyPrimitive(v));
eq +
QueryString.escape(stringifyPrimitive(v));
}).join(sep); }).join(sep);
} else { } else {
return QueryString.escape(stringifyPrimitive(k)) + return ks + QueryString.escape(stringifyPrimitive(obj[k]));
eq +
QueryString.escape(stringifyPrimitive(obj[k]));
} }
}).join(sep); }).join(sep);
default: }
if (!name) return ''; if (!name) return '';
return QueryString.escape(stringifyPrimitive(name)) + eq + return QueryString.escape(stringifyPrimitive(name)) + eq +
QueryString.escape(stringifyPrimitive(obj)); QueryString.escape(stringifyPrimitive(obj));
}
}; };
// Parse a key=val string. // Parse a key=val string.
QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
sep = sep || '&'; sep = sep || '&';
eq = eq || '='; eq = eq || '=';
var obj = {}, var obj = {};
maxKeys = 1000;
// Handle maxKeys = 0 case
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}
if (typeof qs !== 'string' || qs.length === 0) { if (typeof qs !== 'string' || qs.length === 0) {
return obj; return obj;
@ -178,19 +170,27 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
var regexp = /\+/g; var regexp = /\+/g;
qs = qs.split(sep); qs = qs.split(sep);
var maxKeys = 1000;
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}
var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count // maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0) { if (maxKeys > 0 && len > maxKeys) {
qs = qs.slice(0, maxKeys); len = maxKeys;
} }
for (var i = 0, len = qs.length; i < len; ++i) { for (var i = 0; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'), var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq), idx = x.indexOf(eq),
kstr = x.substring(0, idx), kstr, vstr, k, v;
vstr = x.substring(idx + 1), k, v;
if (kstr === '' && vstr !== '') { if (idx >= 0) {
kstr = vstr; kstr = x.substr(0, idx);
vstr = x.substr(idx + 1);
} else {
kstr = x;
vstr = ''; vstr = '';
} }
@ -204,10 +204,10 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
if (!hasOwnProperty(obj, k)) { if (!hasOwnProperty(obj, k)) {
obj[k] = v; obj[k] = v;
} else if (!Array.isArray(obj[k])) { } else if (Array.isArray(obj[k])) {
obj[k] = [obj[k], v];
} else {
obj[k].push(v); obj[k].push(v);
} else {
obj[k] = [obj[k], v];
} }
} }

Loading…
Cancel
Save