1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function Utility() {}
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var exec = require('child_process').exec;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | Utility.prototype.time = function(date) {
|
26 | return this.microtime(date) / 1000;
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Utility.prototype.microtime = function(date) {
|
35 | if (date instanceof Date) {
|
36 | return date.getTime();
|
37 | } else if (typeof date !== 'undefined') {
|
38 | return new Date(date).getTime();
|
39 | } else {
|
40 | return new Date().getTime();
|
41 | }
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Utility.prototype.date = function(format, date) {
|
51 |
|
52 | if (typeof format === 'undefined' || !format) {
|
53 | format = 'yyyy-mm-dd HH:ii:ss';
|
54 | }
|
55 |
|
56 | if (typeof date === 'undefined' || !date) {
|
57 | date = new Date();
|
58 | } else if (!(date instanceof Date)) {
|
59 | date = new Date(date);
|
60 | }
|
61 |
|
62 | var pad = function pad(n) {
|
63 | n = String(n);
|
64 | return n.length == 1 ? '0'+n : n
|
65 | }
|
66 |
|
67 | var day = date.getDate()
|
68 | ,month = date.getMonth() + 1
|
69 | ,year = date.getFullYear()
|
70 | ,yr = year.toString().substring(2,4)
|
71 | ,min = date.getMinutes()
|
72 | ,hour = date.getHours()
|
73 | ,hr = hour % 12 == 0 ? 12 : hour % 12
|
74 | ,sec = date.getSeconds();
|
75 |
|
76 | format = format.replace('yyyy', year);
|
77 | format = format.replace('yy', yr);
|
78 | format = format.replace('mm', pad(month));
|
79 | format = format.replace('m', month);
|
80 | format = format.replace('dd', pad(day));
|
81 | format = format.replace('d', day);
|
82 |
|
83 | format = format.replace('HH', pad(hour));
|
84 | format = format.replace('H', hour);
|
85 |
|
86 | format = format.replace('hh', pad(hr));
|
87 | format = format.replace('h', hr);
|
88 |
|
89 | format = format.replace('ii', pad(min));
|
90 | format = format.replace('i', min);
|
91 |
|
92 | format = format.replace('ss', pad(sec));
|
93 | format = format.replace('s', sec);
|
94 |
|
95 | return format;
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | Utility.prototype.isNumber = function(n) {
|
105 | return !isNaN(parseFloat(n)) && isFinite(n);
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Utility.prototype._ansicheck;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | Utility.prototype.ansi = function(color, message) {
|
120 | if (typeof this.ansicheck === 'undefined') {
|
121 | exec('tput colors', function(err, stdout, stderr) {
|
122 | if (stdout>0) {
|
123 | this.ansicheck = true;
|
124 | } else {
|
125 | this.ansicheck = false;
|
126 | }
|
127 | }.bind(this));
|
128 | }
|
129 | if (this.ansicheck !== false) {
|
130 | var code, bold = '\033[1m', reset = '\033[0m';
|
131 | switch (color) {
|
132 | case 'green' : code = '\033[32m'; break;
|
133 | case 'yellow' : code = '\033[33m'; break;
|
134 | case 'red' : code = '\033[31m'; break;
|
135 | case 'blue' : code = '\033[34m'; break;
|
136 | }
|
137 | return bold + code + message + reset;
|
138 | }
|
139 | return message;
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | exports = module.exports = Utility; |
\ | No newline at end of file |