UNPKG

3.67 kBJavaScriptView Raw
1/******************************************************************************
2 * Lemonade JS
3 * https://lemonadejs.com
4 *
5 * ----
6 *
7 * Utility
8 *
9 * Common operations
10 *
11 *****************************************************************************/
12
13function Utility() {}
14
15/**
16 * Dependencies
17 */
18var exec = require('child_process').exec;
19
20/**
21 * Current unixtime
22 * @param {date|string|integer} date
23 * @return {number}
24 */
25Utility.prototype.time = function(date) {
26 return this.microtime(date) / 1000;
27};
28
29/**
30 * Current microtime
31 * @param {date|string|integer} date
32 * @return {number}
33 */
34Utility.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 * Format date
46 * @param {string} format
47 * @param {Date|string|integer} date
48 * @return {string}
49 */
50Utility.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)); //24h format with leading 0
84 format = format.replace('H', hour); //24h format without leading 0
85
86 format = format.replace('hh', pad(hr)); //12h format with leading 0
87 format = format.replace('h', hr); //12h format without leading 0
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 * Check if input is a numberic value
100 * http://stackoverflow.com/a/1830844/356446
101 * @param n
102 * @returns {boolean}
103 */
104Utility.prototype.isNumber = function(n) {
105 return !isNaN(parseFloat(n)) && isFinite(n);
106};
107
108/**
109 * Used to store if the console can handle colours
110 */
111Utility.prototype._ansicheck;
112
113/**
114 * Add some colors to our console output
115 * @param {string} color
116 * @param {string} message
117 * @return {string}
118 */
119Utility.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 * Export
144 */
145exports = module.exports = Utility;
\No newline at end of file