aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2010-07-25 15:53:14 +0200
committerMatěj Cepl <mcepl@redhat.com>2010-07-25 15:55:49 +0200
commit2765f8e3fb9a008e681371357d88d9b636679b78 (patch)
treeccdfdf8fa946d4aca6a60a6bd168503452931cd3
parent3a51ae92a501e9eb8dc1bed4eb1419473aba4604 (diff)
downloadbugzilla-triage-2765f8e3fb9a008e681371357d88d9b636679b78.tar.gz
Add util.getParamsFromURL method to parse URL parameters.
-rw-r--r--lib/util.js36
-rw-r--r--tests/test-util.js39
2 files changed, 62 insertions, 13 deletions
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;
};
/**
diff --git a/tests/test-util.js b/tests/test-util.js
index b10d485..fcf840c 100644
--- a/tests/test-util.js
+++ b/tests/test-util.js
@@ -3,6 +3,7 @@
// TODO: add some failing tests as well
"use strict";
var util = require("util");
+var urlMod = require("url");
// testing util.heir
exports.ensureHeir = function (test) {
@@ -170,12 +171,6 @@ exports.ensureCSVRemoveEmpty = function (test) {
};
-// testing util.getBugNo
-exports.ensureGetBugNo = function (test) {
- var bugNo = util.getBugNo("https://bugzilla.redhat.com/show_bug.cgi?id=597141");
- test.assertEqual(bugNo, 597141, "getting bug number");
-};
-
// testing util.getObjectKeys
exports.ensureGetObjectKeys = function (test) {
var testObj = {
@@ -185,4 +180,34 @@ exports.ensureGetObjectKeys = function (test) {
test.assertEqual(JSON.stringify(util.getObjectKeys(testObj)),
JSON.stringify(["a", "b"]),
"getting keys from a object");
-}; \ No newline at end of file
+};
+
+// testing util.getParamsFromURL
+exports.ensureGetParamsFromURL = function (test) {
+ test.assertEqual(JSON.stringify(util.getParamsFromURL("https://bugzilla.redhat.com/show_bug.cgi?id=549066")),
+ JSON.stringify({id:"549066"}),
+ "simply compare result of bugzilla show_page URL");
+ var complexURL = new urlMod.URL("http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient"+
+ "&gfns=1&q=gg+javascript+url+parse");
+ test.assertEqual(JSON.stringify(util.getParamsFromURL(complexURL)),
+ JSON.stringify({
+ "ie":"UTF-8",
+ "oe":"UTF-8",
+ "sourceid":"navclient",
+ "gfns":"1",
+ "q":"gg+javascript+url+parse"
+ }),
+ "simply compare result of bugzilla show_page URL");
+ test.assertEqual(JSON.stringify(util.getParamsFromURL("https://bugzilla.redhat.com/")),
+ JSON.stringify(null),
+ "URL without any parameters");
+ test.assertEqual(JSON.stringify(util.getParamsFromURL("")),
+ JSON.stringify(null),
+ "no URL");
+};
+
+// testing util.getBugNo
+exports.ensureGetBugNo = function (test) {
+ var bugNo = util.getBugNo("https://bugzilla.redhat.com/show_bug.cgi?id=597141");
+ test.assertEqual(bugNo, 597141, "getting bug number");
+};