aboutsummaryrefslogtreecommitdiffstats
path: root/lib/util.js
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2010-05-31 22:19:07 +0200
committerMatěj Cepl <mcepl@redhat.com>2010-05-31 22:19:07 +0200
commit93570dc0f18dbe255cf453c947c5d521acae9c69 (patch)
treefe3cdd73196087d6c988a0fcc05cd5bbf2768998 /lib/util.js
parent475c2df048fa5c42ba913ceeba3e6201938031f7 (diff)
downloadbugzilla-triage-93570dc0f18dbe255cf453c947c5d521acae9c69.tar.gz
* Added hopefully a full coverage of unit tests for:
util.valToArray util.addCSVValue util.removeCSVValue * Cleaning up to make jslint happy for */util*.js
Diffstat (limited to 'lib/util.js')
-rw-r--r--lib/util.js45
1 files changed, 25 insertions, 20 deletions
diff --git a/lib/util.js b/lib/util.js
index 3416b7b..c57cb1b 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -1,4 +1,5 @@
-/*global exports, require */
+/*global exports: false, require: false */
+/*jslint onevar: false */
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";
@@ -27,7 +28,7 @@ var urlMod = require("url");
* Son.prototype.constructor = Son;
* </pre>
*/
-exports.heir = function heir (p) {
+exports.heir = function heir(p) {
function f() {};
f.prototype = p.prototype;
return new f();
@@ -41,7 +42,7 @@ exports.heir = function heir (p) {
* @param list list
* @return position of the string in the list, or -1 if none found.
*/
-var isInList = exports.isInList = function isInList (mbr, list) {
+var isInList = exports.isInList = function isInList(mbr, list) {
if (!list) {
return false;
}
@@ -54,7 +55,7 @@ var isInList = exports.isInList = function isInList (mbr, list) {
* @param date
* @return string with the formatted date
*/
-exports.getISODate = function getISODate (dateStr) {
+exports.getISODate = function getISODate(dateStr) {
function pad(n) {
return n < 10 ? '0' + n : n;
}
@@ -84,11 +85,17 @@ var valToArray = exports.valToArray = function valToArray(val) {
* @return String with merged lists
*/
exports.addCSVValue = function addCSVValue(str, value) {
- var parts = (str.trim().length > 0 ? str.split(",") : []);
- if (!isInList(value,parts)) {
+ 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(", ");
};
@@ -101,12 +108,12 @@ exports.addCSVValue = function addCSVValue(str, value) {
*/
exports.removeCSVValue = function removeCSVValue(str, value) {
str = str.trim();
- var parts = str ? str.split(",") : [];
- var valueArr = value instanceof Array ? value : value.split(",");
- parts = parts.filter(function (e,i,a) {
- return (isInList(e,valueArr));
+ 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(",");
+ return parts.join(", ");
};
/**
@@ -114,8 +121,9 @@ exports.removeCSVValue = function removeCSVValue(str, value) {
*
* @param url String with the investigated URL
* @return String with hostname
+ * @depreceated
*/
-exports.getHost = function getHost (url) {
+exports.getHost = function getHost(url) {
var uri = urlMod.parse(url);
return uri.host;
};
@@ -123,10 +131,7 @@ exports.getHost = function getHost (url) {
/**
*
*/
-var getBugNo = exports.getBugNo = function getBugNo (url) {
-// var bugNoTitle = this.doc.querySelector("#title > p").textContent.trim();
-// console.log("bugNoTitle = " + bugNoTitle);
-// this.bugNo = this.getBugNo(this.doc.location.toString());
+var getBugNo = exports.getBugNo = function getBugNo(url) {
var re = new RegExp(".*id=([0-9]+).*$");
var bugNo = null;
var reResult = re.exec(url);
@@ -145,7 +150,7 @@ var getBugNo = exports.getBugNo = function getBugNo (url) {
* @param what optional Object argument representing this for this call
* @return none
*/
-var loadText = exports.loadText = function loadText (URL, cb_function, what) {
+var loadText = exports.loadText = function loadText(URL, cb_function, what) {
if (what === undefined) { // missing optional argument
what = this;
}
@@ -153,8 +158,8 @@ var loadText = exports.loadText = function loadText (URL, cb_function, what) {
var req = new xhrMod.XMLHttpRequest();
req.open("GET", URL, true);
req.onreadystatechange = function (aEvt) {
- if (req.readyState == 4) {
- if (req.status == 200) {
+ if (req.readyState === 4) {
+ if (req.status === 200) {
cb_function.call(what, req.responseText);
} else {
throw "Getting " + URL + "failed!";
@@ -173,7 +178,7 @@ var loadText = exports.loadText = function loadText (URL, cb_function, what) {
* @param what optional Object argument representing this for this call
* @return none
*/
-exports.loadJSON = function loadJSON (URL, cb_function, what) {
+exports.loadJSON = function loadJSON(URL, cb_function, what) {
if (what === undefined) { // missing optional argument
what = this;
}