UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getColumn = exports.pad = exports.removeStyles = exports.maxTableColumnLength = exports.getBrowserStyle = exports.ANSICodes = exports.stringColorToAnsiColor = exports.SetLoggerEnvironment = exports.defaultLogTableOptions = exports.isBrowser = void 0;
4var s_color_1 = require("s.color");
5exports.isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
6exports.defaultLogTableOptions = { padding: 0, spacing: 1 };
7/**
8 * Can be used to change the assumed environment
9 */
10function SetLoggerEnvironment(env) {
11 exports.isBrowser = env === 'browser';
12}
13exports.SetLoggerEnvironment = SetLoggerEnvironment;
14function stringColorToAnsiColor(type, color) {
15 if (!color) {
16 return undefined;
17 }
18 var _a = s_color_1.StringToRGB(color, true), r = _a.r, g = _a.g, b = _a.b;
19 return ANSICodes(type) + "2;" + r + ";" + g + ";" + b + ";";
20}
21exports.stringColorToAnsiColor = stringColorToAnsiColor;
22function ANSICodes(type) {
23 switch (type) {
24 case 'reset':
25 return '0';
26 case 'bold':
27 return '1;';
28 case 'color':
29 return '38;';
30 case 'background':
31 return '48;';
32 }
33}
34exports.ANSICodes = ANSICodes;
35function getBrowserStyle(style) {
36 if (style == undefined)
37 return '';
38 if (typeof style === 'string') {
39 return "color: " + style + ";";
40 }
41 var out = '';
42 for (var key in style) {
43 if (Object.prototype.hasOwnProperty.call(style, key)) {
44 out += key + ": " + style[key] + "; ";
45 }
46 }
47 return out;
48}
49exports.getBrowserStyle = getBrowserStyle;
50function maxTableColumnLength(column) {
51 var max = 0;
52 for (var i = 0; i < column.length; i++) {
53 var field = column[i];
54 if (field) {
55 var length_1 = removeStyles(typeof field === 'object' ? field.message : field).length;
56 max = length_1 > max ? length_1 : max;
57 }
58 }
59 return max;
60}
61exports.maxTableColumnLength = maxTableColumnLength;
62function removeStyles(item) {
63 return item.toString().replace(/[\033\x1b\u001b]\[.*?m/g, '');
64}
65exports.removeStyles = removeStyles;
66function pad(text, start, end) {
67 var space = function (amount) { return ' '.repeat(amount); };
68 return "" + space(start) + text + space(end);
69}
70exports.pad = pad;
71function getColumn(matrix, col) {
72 return matrix.map(function (row) { return row[col]; });
73}
74exports.getColumn = getColumn;