Improved query string handling

This commit is contained in:
Michael Mathews 2011-01-09 17:51:37 +00:00
parent e51f8533bd
commit e1ffad5694
2 changed files with 43 additions and 7 deletions

View File

@ -123,13 +123,7 @@ function main() {
env.opts = jsdoc.opts.parser.parse(env.args);
if (env.opts.query) {
var q = env.opts.query;
env.opts.query = {};
var queryString = {};
q.replace( // thanks Steven Benner
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { env.opts.query[$1] = $3; }
);
env.opts.query = require('common/query').toObject(env.opts.query);
}
if (env.opts.help) {

42
modules/common/query.js Normal file
View File

@ -0,0 +1,42 @@
/**
Support parsing of command line querystrings into JS objects.
@module common/query
@example
-q 'format=xml&root+node=documentation&publish='
> becomes
{
"format": "xml",
"root node": "documentation",
"publish": true
}
*/
(function() {
var query = module.exports = {
/**
@name module:common/query.toObject
@param {string} querystring
@returns {object}
*/
toObject: function(querystring) {
var object = {};
querystring.replace(
new RegExp('([^?=&]+)(?:=([^&]*))?', 'g'),
function($0, key, val) {
object[query._decode(key)] =
(typeof val !== 'undefined')? query._decode(val) : true;
}
);
return object;
},
/** @private */
_decode: function(string) {
return decodeURIComponent( string.replace(/\+/g, ' ') );
}
};
})();