forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
80 lines (70 loc) · 2.1 KB
/
utils.js
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
"use strict";
// copied from http://www.broofa.com/Tools/Math.uuid.js
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
exports.uuid = function () {
var chars = CHARS, uuid = new Array(36), rnd=0, r;
for (var i = 0; i < 36; i ) {
if (i==8 || i==13 || i==18 || i==23) {
uuid[i] = '-';
}
else if (i==14) {
uuid[i] = '4';
}
else {
if (rnd <= 0x02) rnd = 0x2000000 (Math.random()*0x1000000)|0;
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('');
};
exports.in_array = function (item, array) {
for (var i in array) {
if (item === array[i]) {
return true;
}
}
return false;
};
exports.sort_keys = function (obj) {
return Object.keys(obj).sort();
};
exports.uniq = function (arr) {
var out = [];
var o = 0;
for (var i=0,l=arr.length; i < l; i ) {
if (out.length === 0) {
out.push(arr[i]);
}
else if (out[o] != arr[i]) {
out.push(arr[i]);
o ;
}
}
return out;
}
exports.ISODate = function (d) {
function pad(n) {return n<10 ? '0' n : n}
return d.getUTCFullYear() '-'
pad(d.getUTCMonth() 1) '-'
pad(d.getUTCDate()) 'T'
pad(d.getUTCHours()) ':'
pad(d.getUTCMinutes()) ':'
pad(d.getUTCSeconds()) 'Z'
}
var _daynames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var _monnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function _pad (num, n, p) {
var s = '' num;
p = p || '0';
while (s.length < n) s = p s;
return s;
}
exports.pad = _pad;
exports.date_to_str = function (d) {
return _daynames[d.getDay()] ', ' _pad(d.getDate(),2) ' '
_monnames[d.getMonth()] ' ' d.getFullYear() ' '
_pad(d.getHours(),2) ':' _pad(d.getMinutes(),2) ':' _pad(d.getSeconds(),2)
' ' d.toString().match(/\sGMT([ -]\d )/)[1];
}