aboutsummaryrefslogtreecommitdiffstats
path: root/lib/util.js
blob: 18a3c1b18375ffbfae0a18a1e604630154aedf76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*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,Ci} = require("chrome");
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 = heir(Father);
 * Son.prototype.constructor = Son;
 * </pre>
 */
exports.heir = function heir(p) {
    function F() {};
    F.prototype = p.prototype;
    return new F();
};

/**
 * get parameters of URL as an object (name, value)
 */
var getParamsFromURL = exports.getParamsFromURL = 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;
};

/**
 * FIXME this should go to bzpage.js ... no reason to keep it here
 */
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());
};

/**
 * 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);
};

/**
 * 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) {
    var 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 "";
    }
};

/**
 * remove elements from the page based on their IDs
 *
 * @param doc Document object
 * @param target String/Array with ID(s)
 * @param remove Boolean indicating whether the node should be
 * actually removed or just hidden.
 * @return none
 * TODO remove parameter could be replaced by function which would
 * do actual activity.
 */
exports.killNodes = function killNodes(doc, target, remove) {
    target = target.trim();
    var targetArr = target instanceof Array ? target : target.split(/,\s*/);
    targetArr.forEach(function(x) {
        if (remove) {
            var targetNode = doc.getElementById(x);
            targetNode.parentNode.removeChild(targetNode);
        } else {
            x.style.display = "none";
        }
    });
};

exports.getObjectKeys = function getObjectKeys(obj) {
    var keys = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            keys.push(key);
        }
    }
    return keys;
};