From e1ffad56944bf86d764a692a37ff8a14af0efc11 Mon Sep 17 00:00:00 2001 From: Michael Mathews Date: Sun, 9 Jan 2011 17:51:37 +0000 Subject: [PATCH] Improved query string handling --- main.js | 8 +------- modules/common/query.js | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 modules/common/query.js diff --git a/main.js b/main.js index 1810b1e5..f43a66a8 100644 --- a/main.js +++ b/main.js @@ -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) { diff --git a/modules/common/query.js b/modules/common/query.js new file mode 100644 index 00000000..4c712ece --- /dev/null +++ b/modules/common/query.js @@ -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, ' ') ); + } + }; +})(); \ No newline at end of file