aboutsummaryrefslogtreecommitdiffstats
path: root/lib/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util.js')
-rw-r--r--lib/util.js185
1 files changed, 185 insertions, 0 deletions
diff --git a/lib/util.js b/lib/util.js
new file mode 100644
index 0000000..66976fd
--- /dev/null
+++ b/lib/util.js
@@ -0,0 +1,185 @@
+/*global exports, require */
+// Released under the MIT/X11 license
+// http://www.opensource.org/licenses/mit-license.php
+"use strict";
+// ==============================================================
+var xhrMod = require("xhr");
+var urlMod = require("url");
+
+/**
+ * Function for the management of the prototypal inheritace
+ * David Flanagan, Javascript: The Definitve Guide,
+ * IV. edition, O'Reilly, 2006, p. 168
+ *
+ * @param superobject
+ * @return new object, it needs new prototype.constructor
+ *
+ * <pre>
+ * function Father(x) {
+ * this.family = x;
+ * }
+ *
+ * function Son(x,w) {
+ * Father.call(this,x);
+ * this.wife = w;
+ * }
+ * Son.prototype = hlpr.heir(Father);
+ * Son.prototype.constructor = Son;
+ * </pre>
+ */
+exports.heir = function heir (p) {
+ function f() {};
+ f.prototype = p.prototype;
+ return new f();
+};
+
+/**
+ * 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);
+};
+
+/**
+ * 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) {
+ return (val instanceof Array) ? 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(",") : []);
+ if (!isInList(value,parts)) {
+ var newValue = valToArray(value);
+ parts = parts.concat(newValue);
+ }
+ 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(",") : [];
+ var valueArr = value instanceof Array ? value : value.split(",");
+ parts = parts.filter(function (e,i,a) {
+ return (isInList(e,valueArr));
+ });
+ return parts.join(",");
+};
+
+/**
+ * From given URL returns hostname of the server
+ *
+ * @param url String with the investigated URL
+ * @return String with hostname
+ */
+exports.getHost = function getHost (url) {
+ var uri = urlMod.parse(url);
+ return uri.host;
+};
+
+/**
+ *
+ */
+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 re = new RegExp(".*id=([0-9]+).*$");
+ var bugNo = null;
+ var reResult = re.exec(url);
+ if (reResult[1]) {
+ bugNo = reResult[1];
+ }
+ return bugNo;
+};
+
+/**
+ * Load text from URL
+ *
+ * @param URL String
+ * @param cb_function Function to be called when the download happens with
+ * the downloaded body of the HTTP message as the only parameter
+ * @param what optional Object argument representing this for this call
+ * @return none
+ */
+var loadText = exports.loadText = function loadText (URL, cb_function, what) {
+ if (what === undefined) { // missing optional argument
+ what = this;
+ }
+
+ var req = new xhrMod.XMLHttpRequest();
+ req.open("GET", URL, true);
+ req.onreadystatechange = function (aEvt) {
+ if (req.readyState == 4) {
+ if (req.status == 200) {
+ cb_function.call(what, req.responseText);
+ } else {
+ throw "Getting " + URL + "failed!";
+ }
+ }
+ };
+ req.send("");
+};
+
+/**
+ * Load JSON from URL
+ *
+ * @param URL String
+ * @param cb_function Function to be called when the download happens with
+ * the downloaded JSON as the only parameter
+ * @param what optional Object argument representing this for this call
+ * @return none
+ */
+exports.loadJSON = function loadJSON (URL, cb_function, what) {
+ if (what === undefined) { // missing optional argument
+ what = this;
+ }
+
+ loadText(URL, function (text) {
+ var data = JSON.parse(text);
+ cb_function.call(what, data);
+ }, what);
+};