diff options
author | Matěj Cepl <mcepl@redhat.com> | 2011-02-01 16:28:51 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2011-02-01 16:28:51 +0100 |
commit | c1ec772b104316530f6737340355205398df33af (patch) | |
tree | 98385a10209cef7ee528fb2d536ef5c15bdf0d7e /lib/util.js | |
parent | ab4f262d119fefe06bea938400ee8ff64bed7ab1 (diff) | |
download | bugzilla-triage-c1ec772b104316530f6737340355205398df33af.tar.gz |
This actually almost looks like working, so much I can file bugs.
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..8556951 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,65 @@ +/*global exports: false, require: false, console: false, Cc: false, Ci: false */ +/*jslint onevar: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; +// ============================================================== +var Cc = require("chrome").Cc; +var Ci = require("chrome").Ci; +var urlMod = require("url"); + +/** + * get parameters of URL as an object (name, value) + */ +function getParamsFromURL (url, base) { + if (!url || (url.toString().length === 0)) { + throw new Error("Missing URL value!"); + } + + if (!(url instanceof urlMod.URL)) { + url = new urlMod.URL(url.toString(), base); + } + + var paramsArr = url.path.split("?"); + if (paramsArr.length === 1) { + return {}; + } + + // get convert URL parameters to an Object + var params = {}, s = []; + paramsArr[1].split('&').forEach(function(par) { + s = par.split('='); + params[s[0]] = s[1]; + }); + return params; +} + +/** + * Get a bug no from URL ... fails with aliases + * It should theoretically belong to bzpage.js, but we don't have + * unit tests there yet, so keeping here. + * + * @param url String with URL to be analyzed + * @return String with the bug ID (hopefully number, but not for aliases) + */ +exports.getBugNo = function getBugNo(url) { + var params = getParamsFromURL(url); + if (params && params.id) { + return params.id; + } +}; + +/** + * format date to be in ISO format (just day part) + * + * @param date + * @return string with the formatted date + */ +exports.getISODate = function getISODate(dateStr) { + function pad(n) { + return n < 10 ? '0' + n : n; + } + var date = new Date(dateStr); + return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + + pad(date.getDate()); +}; |