From 2765f8e3fb9a008e681371357d88d9b636679b78 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sun, 25 Jul 2010 15:53:14 +0200 Subject: Add util.getParamsFromURL method to parse URL parameters. --- lib/util.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/util.js b/lib/util.js index 4f3964e..d8da1e5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,6 +5,7 @@ "use strict"; // ============================================================== var {Cc,Ci} = require("chrome"); +var urlMod = require("url"); /** * Function for the management of the prototypal inheritace @@ -33,17 +34,40 @@ exports.heir = function heir(p) { return new F(); }; +/** + * get parameters of URL as an object (name, value) + */ +var getParamsFromURL = exports.getParamsFromURL = function getParamsFromURL (url) { + if (!url || (url.toString().length === 0)) { + return null; + } + + if (!(url instanceof urlMod.URL)) { + url = new urlMod.URL(url.toString()); + } + + if (url.path === "/") { + return null; + } + + var paramsArr = url.path.split("?")[1].split('&'); + // get convert URL parameters to an Object + var params = {}, s = []; + paramsArr.forEach(function(par) { + s = par.split('='); + params[s[0]] = s[1]; + }); + return params; +}; + exports.getBugNo = function getBugNo(url) { - var re = new RegExp(".*id=([0-9]+).*$"); - var bugNo = null; if (!url) { throw new Error("Missing URL value!"); } - var reResult = re.exec(url); - if (reResult && reResult[1]) { - bugNo = reResult[1]; + var params = getParamsFromURL(url); + if (params && params.id) { + return parseInt(params.id, 10); } - return bugNo; }; /** -- cgit