Skip to content
Browse files

Use the parser in the http_parser binding instead of decodeURIComponent

  • Loading branch information...
1 parent 38f6a93 commit dd5ec6563387dd06c1421790d01a08201078323f @isaacs committed Jun 14, 2010
Showing with 10 additions and 20 deletions.
  1. +10 −20 lib/querystring.js
View
30 lib/querystring.js
@@ -1,10 +1,10 @@
// Query String Utilities
var QueryString = exports;
+var urlDecode = process.binding('http_parser').urlDecode;
-QueryString.unescape = function (str, decodeSpaces) {
- return decodeURIComponent(decodeSpaces ? str.replace(/\+/g, " ") : str);
-};
+// a safe fast alternative to decodeURIComponent
+QueryString.unescape = urlDecode;
QueryString.escape = function (str) {
return encodeURIComponent(str);
@@ -25,17 +25,17 @@ var stack = [];
* @param name {String} (optional) Name of the current key, for handling children recursively.
* @static
*/
-QueryString.stringify = function (obj, sep, eq, munge, name) {
+QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name) {
munge = typeof(munge) == "undefined" ? true : munge;
sep = sep || "&";
eq = eq || "=";
if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') {
- return name ? encodeURIComponent(name) + eq : '';
+ return name ? QueryString.escape(name) + eq : '';
}
if (isBool(obj)) obj = +obj;
if (isNumber(obj) || isString(obj)) {
- return encodeURIComponent(name) + eq + encodeURIComponent(obj);
+ return QueryString.escape(name) + eq + QueryString.escape(obj);
}
if (isA(obj, [])) {
var s = [];
@@ -71,11 +71,11 @@ QueryString.stringify = function (obj, sep, eq, munge, name) {
return s;
};
-QueryString.parseQuery = QueryString.parse = function (qs, sep, eq) {
+QueryString.parse = QueryString.decode = function (qs, sep, eq) {
return (qs || '')
.split(sep||"&")
.map(pieceParser(eq||"="))
- .reduce(mergeParams);
+ .reduce(mergeParams)
};
// Parse a key=val string.
@@ -87,27 +87,17 @@ QueryString.parseQuery = QueryString.parse = function (qs, sep, eq) {
// return parse(foo[bar], [{bla:"baz"}])
// return parse(foo, {bar:[{bla:"baz"}]})
// return {foo:{bar:[{bla:"baz"}]}}
-var trimmerPattern = /^\s+|\s+$/g,
- slicerPattern = /(.*)\[([^\]]*)\]$/;
+var slicerPattern = /(.*)\[([^\]]*)\]$/;
var pieceParser = function (eq) {
return function parsePiece (key, val) {
if (arguments.length !== 2) {
// key=val, called from the map/reduce
key = key.split(eq);
return parsePiece(
QueryString.unescape(key.shift(), true),
- QueryString.unescape(key.join(eq), true)
+ ''+QueryString.unescape(key.join(eq), true)
);
}
- key = key.replace(trimmerPattern, '');
- if (isString(val)) {
- val = val.replace(trimmerPattern, '');
- // convert numerals to numbers
- if (!isNaN(val)) {
- var numVal = +val;
- if (val === numVal.toString(10)) val = numVal;
- }
- }
var sliced = slicerPattern.exec(key);
if (!sliced) {
var ret = {};

0 comments on commit dd5ec65

Please sign in to comment.
Something went wrong with that request. Please try again.