-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
66 lines (53 loc) · 1.79 KB
/
util.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
const clc = require('cli-color');
const paddToFitWidth = (string = '', width) => {
const sLength = string.length;
if (!width) width = sLength + 2; // one space on each side
const halfWith = Math.ceil((width - sLength) / 2);
const paddStr = ' '.repeat(halfWith);
return paddStr + string + paddStr;
};
const cappitalize = (string) => {
return string.charAt(0).toUpperCase()
+ string.substring(1, string.length);
};
const formatters = {
bold: (clc, s) => clc.bold(s),
italic: (clc, s) => clc.italic(s),
inverse: (clc, s) => clc.inverse(s),
blink: (clc, s) => clc.blink(s),
strike: (clc, s) => clc.strike(s),
underline: (clc, s) => clc.underline(s),
};
const validXtermColor = (val) => (val < 255 && val > 0);
const isString = (val) => typeof val === 'string';
const colorExists = (val) => val in clc;
const bgColorExists = (val) => `bg${cappitalize(val)}` in clc;
const getBgColor = (clc, color) => {
const isValidNumber = validXtermColor(color);
const isValidString = isString(color) && bgColorExists(color);
if (isValidNumber) return clc.bgXterm(color);
if (isValidString) return clc[`bg${cappitalize(color)}`];
return clc.bgBlue;
};
const getTextColor = (clc, color) => {
const isValidNumber = validXtermColor(color);
const isValidString = isString(color) && colorExists(color);
if (isValidNumber) return clc.xterm(color);
if (isValidString) return clc[color];
return clc.white;
};
const format = (clc, string, formatterName) => {
const formatter = formatters[formatterName];
return formatter ? formatter(clc, string) : clc(string);
};
module.exports = {
paddToFitWidth,
cappitalize,
getBgColor,
getTextColor,
format,
validXtermColor,
isString,
colorExists,
bgColorExists,
}