-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_tools.js
More file actions
executable file
·102 lines (86 loc) · 2.19 KB
/
generic_tools.js
File metadata and controls
executable file
·102 lines (86 loc) · 2.19 KB
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
// Message format
defaultMapFormats = {
formatCompact:"dt, u: m",
formatLarge:"u [dt]\\nm\\n",
formatCustom:"",
selected:"formatCompact",
};
function formatMsg(format, datetime, username, text){
var msg = format;
var mapObj = {
dt:datetime,
d:datetime.substring(0,10),
t:datetime.substring(11,19),
u:username,
m:text,
};
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
msg = msg.replace(re, function(matched){
return mapObj[matched];
});
return msg;
}
function prepareFormat(format) {
format = format.replace(/\\n/g, "\n");
format = format.replace(/\\t/g, "\t");
return format;
}
// Argument is of type Date.
// Some examples:
// d = new Date(); // current date
// d.setMonth(d.getMonth() - 3); // set 3 month prior to date
function formatDate(d){
return lead(d.getDate())+'.'+lead(d.getMonth()+1)+'.'+d.getFullYear() + ' '
+ lead(d.getHours()) + ':' + lead(d.getMinutes()) + ':' + lead(d.getSeconds());
//Example of output 31.12.2016 23:59:59
}
function formatDateForFileName(d){
return d.getFullYear() + '_' +lead(d.getMonth()+1)+'_' + lead(d.getDate()) +'--'
+ lead(d.getHours()) + '-' + lead(d.getMinutes()) + '-' + lead(d.getSeconds());
}
function lead(a){
var s = '0' + a;
if (s.length>2)
s = s.substr(1);
return s;
}
function friendlySize(size){
var s = '' + size
var res = s
for(var i=s.length-3; i>0; i-=3){
res = res.slice(0,i) + "'" + res.slice(i)
}
return res
}
function appendWithSpace(input_string, new_part){
if (new_part.length == 0)
return input_string
if (input_string.length > 0)
return input_string + ' ' + new_part
return input_string + new_part
}
function comparatorArithmetic(a,b){
return a-b
}
// var arr = [3,4,6]
// binSearch(arr, 5, comparator)
function binSearch(arr, val, comparator) {
if (comparator == null){
comparator = comparatorArithmetic
}
var left = -1
var right = arr.length + 1
while (right - left > 1){
var mid = Math.floor((right + left) / 2)
var cmp = comparator(arr[mid], val)
if (cmp == 0){
return mid
}
if (cmp < 0){
left = mid
}else{
right = mid
}
}
return -right
}