]> git.r.bdr.sh - rbdr/cologne/blob - lib/utilities.js
Delete npmignore
[rbdr/cologne] / lib / utilities.js
1 'use strict';
2
3 const internals = {
4 kCircularString: '[Circular]',
5 kAnsiCodes: {
6 bold: '[1m',
7 italics: '[3m',
8 underline: '[4m',
9 inverse: '[7m',
10 strikethrough: '[9m',
11 bold_off: '[22m',
12 italics_off: '[23m',
13 underline_off: '[24m',
14 inverse_off: '[27m',
15 strikethrough_off: '[29m',
16 black: '[30m',
17 red: '[31m',
18 green: '[32m',
19 yellow: '[33m',
20 blue: '[34m',
21 magenta: '[35m',
22 cyan: '[36m',
23 white: '[37m',
24 default: '[39m',
25 black_bg: '[40m',
26 red_bg: '[41m',
27 green_bg: '[42m',
28 yellow_bg: '[43m',
29 blue_bg: '[44m',
30 magenta_bg: '[45m',
31 cyan_bg: '[46m',
32 white_bg: '[47m',
33 default_bg: '[49m',
34 reset: '[0m'
35 },
36
37 // Get High Res time depending on platform. Currently only supporting node.
38
39 getHighResTime() {
40
41 return process.hrtime.bigint();
42 }
43 };
44
45 // Initialise relative time.
46
47 internals.initialTimestamp = BigInt(Date.now()) * 1000000n;
48 internals.initialHighResTime = internals.getHighResTime();
49
50 /**
51 * Container object for utilities used by loggers.
52 *
53 * @class Utilities
54 */
55 module.exports = {
56
57 /**
58 * Returns the current timestamp in nanoseconds as a bigint.
59 *
60 * @function now
61 * @memberof Utilities
62 * @return {bigint} current time in nanoseconds, including fractions.
63 */
64 now() {
65
66 return internals.initialTimestamp + internals.getHighResTime() - internals.initialHighResTime;
67 },
68
69 /**
70 * Stringifies objects, avoiding circular references.
71 *
72 * @function stringify
73 * @memberof Utilities
74 * @param {Object} object the object to stringify
75 * @return {String} the stringified object
76 */
77 stringify(object) {
78
79 const cache = new Set();
80
81 return JSON.stringify(object, (key, value) => {
82
83 if (typeof value === 'bigint') {
84 return String(value) + 'n';
85 }
86
87 if (typeof value === 'object' && value !== null) {
88 if (cache.has(value)) {
89 return internals.kCircularString;
90 }
91
92 cache.add(value);
93 }
94
95 return value;
96 });
97 },
98
99 /**
100 * Given an ansi keyword, it will return the appropriate code.
101 *
102 * @function getAnsiCode
103 * @memberof Utilities
104 * @param {String} ansiString the name of the desired code
105 * @return {String} the ansi code
106 */
107 getAnsiCode(ansiString) {
108
109 return internals.kAnsiCodes[ansiString] || internals.kAnsiCodes.reset;
110 },
111
112 /**
113 * Given a level, it will return the appropriate ansi keyword related
114 * to it.
115 *
116 * @function getLevelAnsi
117 * @memberof Utilities
118 * @param {number} level the level of the log
119 * @return {String} the ansi keyword
120 */
121 getLevelAnsi(level) {
122
123 switch (level) {
124 case 0:
125 case 1:
126 case 2:
127 case 3:
128 return 'red';
129 case 4:
130 return 'yellow';
131 case 5:
132 case 6:
133 return 'blue';
134 case 7:
135 return 'green';
136 default:
137 return 'default';
138 }
139 }
140 };