style.js
1.56 KB
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
'use strict';
module.exports = {
applyAnsiStyles: applyAnsiStyles,
removeStyles: removeStyles,
transformStyles: transformStyles,
};
var ANSI_COLORS = {
unset: '\x1b[0m',
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
};
function applyAnsiStyles(data) {
return transformStyles(data, styleToAnsi, resetAnsiStyle);
}
function styleToAnsi(style) {
var color = style.replace(/color:\s*(\w+).*/, '$1').toLowerCase();
return ANSI_COLORS[color] || '';
}
function resetAnsiStyle(string) {
return string + ANSI_COLORS.unset;
}
function removeStyles(data) {
return transformStyles(data, function () { return '' });
}
function transformStyles(data, onStyleFound, onStyleApplied) {
var foundStyles = {};
return data.reduce(function (result, item, index, array) {
if (foundStyles[index]) {
return result;
}
if (typeof item === 'string') {
var valueIndex = index;
var styleApplied = false;
item = item.replace(/%[1cdfiOos]/g, function (match) {
valueIndex += 1;
if (match !== '%c') {
return match;
}
var style = array[valueIndex];
if (typeof style === 'string') {
foundStyles[valueIndex] = true;
styleApplied = true;
return onStyleFound(style, item);
}
return match;
});
if (styleApplied && onStyleApplied) {
item = onStyleApplied(item);
}
}
result.push(item);
return result;
}, []);
}