Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

querystring: improved speed and code cleanup

  • Loading branch information...
commit 3a5798b0971467bea8b72a03892121a410e2ff3b 1 parent e74a733
@fb55 fb55 authored isaacs committed
Showing with 38 additions and 38 deletions.
  1. +38 −38 lib/querystring.js
View
76 lib/querystring.js
@@ -134,42 +134,34 @@ var stringifyPrimitive = function(v) {
QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
- obj = (obj === null) ? undefined : obj;
-
- switch (typeof obj) {
- case 'object':
- return Object.keys(obj).map(function(k) {
- if (Array.isArray(obj[k])) {
- return obj[k].map(function(v) {
- return QueryString.escape(stringifyPrimitive(k)) +
- eq +
- QueryString.escape(stringifyPrimitive(v));
- }).join(sep);
- } else {
- return QueryString.escape(stringifyPrimitive(k)) +
- eq +
- QueryString.escape(stringifyPrimitive(obj[k]));
- }
- }).join(sep);
+ if (obj === null) {
+ obj = undefined;
+ }
+
+ if (typeof obj === 'object') {
+ return Object.keys(obj).map(function(k) {
+ var ks = QueryString.escape(stringifyPrimitive(k)) + eq;
+ if (Array.isArray(obj[k])) {
+ return obj[k].map(function(v) {
+ return ks + QueryString.escape(stringifyPrimitive(v));
+ }).join(sep);
+ } else {
+ return ks + QueryString.escape(stringifyPrimitive(obj[k]));
+ }
+ }).join(sep);
- default:
- if (!name) return '';
- return QueryString.escape(stringifyPrimitive(name)) + eq +
- QueryString.escape(stringifyPrimitive(obj));
}
+
+ if (!name) return '';
+ return QueryString.escape(stringifyPrimitive(name)) + eq +
+ QueryString.escape(stringifyPrimitive(obj));
};
// Parse a key=val string.
QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
- var obj = {},
- maxKeys = 1000;
-
- // Handle maxKeys = 0 case
- if (options && typeof options.maxKeys === 'number') {
- maxKeys = options.maxKeys;
- }
+ var obj = {};
if (typeof qs !== 'string' || qs.length === 0) {
return obj;
@@ -178,19 +170,27 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
var regexp = /\+/g;
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
- if (maxKeys > 0) {
- qs = qs.slice(0, maxKeys);
+ if (maxKeys > 0 && len > 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'),
idx = x.indexOf(eq),
- kstr = x.substring(0, idx),
- vstr = x.substring(idx + 1), k, v;
+ kstr, vstr, k, v;
- if (kstr === '' && vstr !== '') {
- kstr = vstr;
+ if (idx >= 0) {
+ kstr = x.substr(0, idx);
+ vstr = x.substr(idx + 1);
+ } else {
+ kstr = x;
vstr = '';
}
@@ -204,10 +204,10 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
if (!hasOwnProperty(obj, k)) {
obj[k] = v;
- } else if (!Array.isArray(obj[k])) {
- obj[k] = [obj[k], v];
- } else {
+ } else if (Array.isArray(obj[k])) {
obj[k].push(v);
+ } else {
+ obj[k] = [obj[k], v];
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.