-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (72 loc) · 2.51 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
const chalk = require('chalk');
module.exports = {
/**
* Collection of flags representing verbosities of the functions
*/
levels : {
/**
* No console output
*/
stfu : 0,
/**
* Minimal console output
*/
quiet : 1,
/**
* Complete console output
*/
verbose : 2
},
/**
* Verbosity of the functions
*/
verbosity: this.levels.verbose,
/** Logs stuff to the console
*
* @param String level : String Defines importance of message ("error", "warning", "log", or none)
* @param String text : Text to log
* @param int indent : a number of tabs before output
* @param boolean output : false = log won't show;
* @return String log : string passed to the console, with coloration and timestamp
*/
log: function(text, level="log", indent=0, output=true){
var d = new Date();
var time = this.lead(d.getHours(),2) + ":" + this.lead(d.getMinutes(),2) + ":" + this.lead(d.getSeconds(),2) + "." + this.lead(d.getMilliseconds(),3);
var log = "[ ";
switch (level) {
case "log":
log += chalk.green("LOG");
break;
case "error":
log += chalk.red("ERR");
break;
case "warning":
log += chalk.yellow("WRN");
break;
default:
log += level;
};
indentText = indent ? " ".repeat(indent-1) + "└───" + "> " : "";
log += " - " + time + " ] " + indentText + text;
if (output) {
if (this.verbosity == this.levels.verbose || (this.verbosity == this.levels.quiet && indent < 1)) {
console.log(log);
}
}
return log;
},
/** Leads a string with a set number of a certain character
*
* @param String string : String to lead
* @param int size : Amount of characters to lead
* @param String char : Character to lead with. Default is "0".
* @return String leadedString : Padded string
*/
lead: function (string, size, char="0") {
var leadedString = string+"";
while (leadedString.length < size){
leadedString = char + leadedString;
}
return leadedString;
}
};