|
@ -1,10 +1,10 @@ |
|
|
// Query String Utilities
|
|
|
// Query String Utilities
|
|
|
|
|
|
|
|
|
var QueryString = exports; |
|
|
var QueryString = exports; |
|
|
|
|
|
var urlDecode = process.binding('http_parser').urlDecode; |
|
|
|
|
|
|
|
|
QueryString.unescape = function (str, decodeSpaces) { |
|
|
// a safe fast alternative to decodeURIComponent
|
|
|
return decodeURIComponent(decodeSpaces ? str.replace(/\+/g, " ") : str); |
|
|
QueryString.unescape = urlDecode; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
QueryString.escape = function (str) { |
|
|
QueryString.escape = function (str) { |
|
|
return encodeURIComponent(str); |
|
|
return encodeURIComponent(str); |
|
@ -25,17 +25,17 @@ var stack = []; |
|
|
* @param name {String} (optional) Name of the current key, for handling children recursively. |
|
|
* @param name {String} (optional) Name of the current key, for handling children recursively. |
|
|
* @static |
|
|
* @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; |
|
|
munge = typeof(munge) == "undefined" ? true : munge; |
|
|
sep = sep || "&"; |
|
|
sep = sep || "&"; |
|
|
eq = eq || "="; |
|
|
eq = eq || "="; |
|
|
if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') { |
|
|
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 (isBool(obj)) obj = +obj; |
|
|
if (isNumber(obj) || isString(obj)) { |
|
|
if (isNumber(obj) || isString(obj)) { |
|
|
return encodeURIComponent(name) + eq + encodeURIComponent(obj); |
|
|
return QueryString.escape(name) + eq + QueryString.escape(obj); |
|
|
} |
|
|
} |
|
|
if (isA(obj, [])) { |
|
|
if (isA(obj, [])) { |
|
|
var s = []; |
|
|
var s = []; |
|
@ -71,11 +71,11 @@ QueryString.stringify = function (obj, sep, eq, munge, name) { |
|
|
return s; |
|
|
return s; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
QueryString.parseQuery = QueryString.parse = function (qs, sep, eq) { |
|
|
QueryString.parse = QueryString.decode = function (qs, sep, eq) { |
|
|
return (qs || '') |
|
|
return (qs || '') |
|
|
.split(sep||"&") |
|
|
.split(sep||"&") |
|
|
.map(pieceParser(eq||"=")) |
|
|
.map(pieceParser(eq||"=")) |
|
|
.reduce(mergeParams); |
|
|
.reduce(mergeParams) |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// Parse a key=val string.
|
|
|
// Parse a key=val string.
|
|
@ -87,26 +87,14 @@ QueryString.parseQuery = QueryString.parse = function (qs, sep, eq) { |
|
|
// return parse(foo[bar], [{bla:"baz"}])
|
|
|
// return parse(foo[bar], [{bla:"baz"}])
|
|
|
// return parse(foo, {bar:[{bla:"baz"}]})
|
|
|
// return parse(foo, {bar:[{bla:"baz"}]})
|
|
|
// return {foo:{bar:[{bla:"baz"}]}}
|
|
|
// return {foo:{bar:[{bla:"baz"}]}}
|
|
|
var trimmerPattern = /^\s+|\s+$/g, |
|
|
var slicerPattern = /(.*)\[([^\]]*)\]$/; |
|
|
slicerPattern = /(.*)\[([^\]]*)\]$/; |
|
|
|
|
|
var pieceParser = function (eq) { |
|
|
var pieceParser = function (eq) { |
|
|
return function parsePiece (key, val) { |
|
|
return function parsePiece (key, val) { |
|
|
if (arguments.length !== 2) { |
|
|
if (arguments.length !== 2) { |
|
|
// key=val, called from the map/reduce
|
|
|
// key=val, called from the map/reduce
|
|
|
key = key.split(eq); |
|
|
key = key.split(eq); |
|
|
return parsePiece( |
|
|
return parsePiece(QueryString.unescape(key.shift(), true), |
|
|
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); |
|
|
var sliced = slicerPattern.exec(key); |
|
|
if (!sliced) { |
|
|
if (!sliced) { |
|
|