diff options
author | Matěj Cepl <mcepl@redhat.com> | 2010-06-19 22:08:04 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2010-06-19 22:08:04 +0200 |
commit | 87b9c2d6d0a1bda477f15af227d758c5fc3e55e0 (patch) | |
tree | 1781a0d60ae99c783add1205722d49da3d2d4143 /lib/util.js | |
parent | 76e764fb8e4f9d52dc92e97c6408c169142b104f (diff) | |
download | bugzilla-triage-87b9c2d6d0a1bda477f15af227d758c5fc3e55e0.tar.gz |
Loads a bug page without crash!!!
There is not jetpack. call in used modules.
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 136 |
1 files changed, 135 insertions, 1 deletions
diff --git a/lib/util.js b/lib/util.js index e14fcb1..7fca362 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,4 @@ -/*global exports: false, require: false */ +/*global exports: false, require: false, Cc: false, Ci: false */ /*jslint onevar: false */ // Released under the MIT/X11 license // http://www.opensource.org/licenses/mit-license.php @@ -49,6 +49,140 @@ var getBugNo = exports.getBugNo = function getBugNo(url) { }; /** + * 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()); +}; + +/** + * Make sure value returned is Array + * + * @param Array/String + * @return Array + * + * If something else than Array or String is passed to the function + * the result will be untouched actual argument of the call. + */ +var valToArray = exports.valToArray = function valToArray(val) { + var isArr = val && + val.constructor && + val.constructor.name === "Array"; + return isArr ? val : [val]; +}; + +/** + * Merges two comma separated string as a list and returns new string + * + * @param str String with old values + * @param value String/Array with other values + * @return String with merged lists + */ +exports.addCSVValue = function addCSVValue(str, value) { + var parts = (str.trim().length > 0 ? str.split(/,\s*/) : []); + if (!value) { + return str; + } + if (!isInList(value, parts)) { + var newValue = valToArray(value); + parts = parts.concat(newValue); + } + // this is necessary to get comma-space separated string even when + // value is an array already + parts = parts.join(",").split(","); + return parts.join(", "); +}; + +/** + * Treats comma separated string as a list and removes one item from it + * + * @param str String treated as a list + * @param value String with the value to be removed from str + * @return String with the resulting list comma separated + */ +exports.removeCSVValue = function removeCSVValue(str, value) { + str = str.trim(); + var parts = str ? str.split(/,\s*/) : []; + var valueArr = value instanceof Array ? value : value.split(/,\s*/); + parts = parts.filter(function (e, i, a) { + return (!isInList(e, valueArr)); + }); + return parts.join(", "); +}; + +/** + * select element of the array where regexp in the first element matches second + * parameter of this function + * + * @param list Array with regexps and return values + * @param chosingMark String by which the element of array is to be matched + * @return Object chosen element + */ +var filterByRegexp = exports.filterByRegexp = + function filterByRegexp(list, chosingMark) { + let chosenPair = []; + if (list.length > 0) { + chosenPair = list.filter(function(pair) { + return new RegExp(pair.regexp, "i").test(chosingMark); + }); + } + if (chosenPair.length > 0) { + return chosenPair[0].addr; + } else { + return ""; + } +}; + +/** + * Check whether an item is member of the list. Idea is just to make long if + * commands slightly more readable. + * + * @param mbr string to be searched in the list + * @param list list + * @return position of the string in the list, or -1 if none found. + */ +var isInList = exports.isInList = function isInList(mbr, list) { + if (!list) { + return false; + } + return (list.indexOf(mbr) !== -1); +}; + +/** + * returns password with a special password + * + * @return String with the password + */ +var getPassword = exports.getPassword = function getPassword() { + var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] + .getService(Ci.nsIPromptService); + var password = { + value : "" + }; // default the password to pass + var check = { + value : true + }; // default the checkbox to true + var result = prompts.promptPassword(null, "Title", "Enter password:", + password, null, check); + // result is true if OK was pressed, false if cancel was pressed. + // password.value is set if OK was pressed. + // The checkbox is not displayed. + if (result) { + return password.value ? password.value : null; + } else { + return undefined; + } +}; + +/** * Load text from URL * * @param URL String |