-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretty.js
253 lines (225 loc) · 7.54 KB
/
pretty.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict';
const process = require('node:process');
//////////
const darkTheme = {
Boolean: '#00875f',
Date: '#d7d75f',
Error: '#af0000',
Function: '#5fd7ff',
Map: '#5fd787',
Null: '#5f00af',
Number: '#875fd7',
Promise: '#5bb761',
PromisePending: '#83c6ea',
PromiseRejected: '#fc6c86',
RegExp: '#ff8787',
Set: '#afd7d7',
String: '#005fd7',
Symbol: '#afaf5f',
Undefined: '#af0087',
Unknown: '#d78700',
circular: 'fg: #af0000; style: bold;',
decoration: '#1c1c1c',
nonEnumerable: '#ff0087',
property: '#8a8a8a',
stack: '#bbbbbb',
};
const defaults = {
all: false,
color: true,
json: false,
lineNumbers: false,
print: true,
showDepth: true,
stream: process.stdout,
theme: darkTheme,
};
const configuration = { ...defaults };
function configure (options = {}) {
Object.assign(configuration, options);
}
function reset () {
Object.assign(configuration, defaults);
}
/**
* Pretty Print any value with colorization.
* @param {*} object : any object or value
* @param {{
* all : boolean|undefined,
* print : boolean|undefined,
* json : boolean|undefined,
* lineNumbers : boolean|undefined,
* showDepth : boolean|undefined,
* theme : Object|undefined }} options
*/
function prettyPrint (object, {
all = configuration.all,
color = configuration.color,
json = configuration.json,
lineNumbers = configuration.lineNumbers,
print = configuration.print,
showDepth = configuration.showDepth,
stream = configuration.stream,
theme = configuration.theme,
} = {}) {
let style;
if (color && stream && stream.isTTY) {
style = require('./style');
} else {
style = (string) => string;
}
function indent (depth) {
return ' '.repeat(depth);
}
function addLineNumbers (output) {
const lines = output.split(/\n/u);
const padding = lines.length.toString().length + 1;
let number = 0;
return lines.map((line) => {
number++;
const lineNumber = `${ ' '.repeat(padding - number.toString().length) +
style(number, theme.property) } \u2502 `;
return lineNumber + line;
}).join('\n');
}
function prettyPrinter (value, level, previous, overrideColor) {
let depth = level;
let line = indent(depth);
const seen = new Set(previous);
if (typeof value === 'object' && seen.has(value)) {
line += style('[Circular Reference]', theme.circular);
} else {
if (typeof value === 'object' && value !== null) {
seen.add(value);
}
if (typeof value === 'string') {
if (overrideColor && !json) {
line += style(value, overrideColor);
} else if (json) {
line += style(`"${ value }"`, overrideColor || theme.String);
} else {
line += style(`'${ value }'`, overrideColor || theme.String);
}
} else if (typeof value === 'number') {
line += style(value, overrideColor || theme.Number);
} else if (typeof value === 'boolean') {
line += style(value, overrideColor || theme.Boolean);
} else if (typeof value === 'undefined') {
line += style(value, theme.Undefined);
} else if (value === null) {
line += style(value, theme.Null);
} else if (value instanceof Error) {
const joiner = indent(depth);
const parts = value.stack.toString().split(/\n/u);
line += style(parts.shift(), theme.Error);
line += `\n${ joiner }`;
line += parts.map((item) => style(item, theme.stack)).join(`\n${ joiner }`);
} else if (value instanceof Date) {
line += style(value.toString(), theme.Date);
} else if (value instanceof RegExp) {
line += style(value.toString(), theme.RegExp);
} else if (typeof value === 'function') {
line += style(value.toString(), theme.Function);
} else if (typeof value === 'symbol') {
line += style(value.toString(), theme.Symbol);
} else if (Array.isArray(value)) {
line += '[';
if (value.length) {
line += '\n';
}
depth++;
for (let i = 0; i < value.length; i++) {
const comma = i < value.length - 1 ? ',' : '';
line += `${ prettyPrinter(value[i], depth, seen) + comma }\n`;
}
depth--;
line += `${ indent(depth) }]`;
} else if (value instanceof WeakMap) {
line += `${ style('WeakMap', theme.Map) } {}`;
} else if (value instanceof Map && !json) {
line += `${ style('Map', theme.Map) } {`;
if (value.size) {
line += '\n';
}
depth++;
let j = 0;
value.forEach((itemValue, key) => {
const comma = j < value.size - 1 ? ',' : '';
line += `${ prettyPrinter(key, depth, seen) }: `;
line += `${ prettyPrinter(itemValue, depth, seen) + comma }\n`;
j++;
});
depth--;
line += `${ indent(depth) }}`;
} else if (value instanceof WeakSet) {
line += `${ style('WeakSet', theme.Set) } []`;
} else if (value instanceof Set && !json) {
line += `${ style('Set', theme.Set) } [`;
if (value.size) {
line += '\n';
}
depth++;
let j = 0;
value.forEach((itemValue) => {
const comma = j < value.size - 1 ? ',' : '';
line += `${ prettyPrinter(itemValue, depth, seen) + comma }\n`;
j++;
});
depth--;
line += `${ indent(depth) }]`;
} else if (value instanceof Promise) {
const details = process.binding('util').getPromiseDetails(value);
if (details[0] === 0) {
line += `${ style('Promise', theme.Promise) } { ${ style('pending', theme.PromisePending) } }`;
} else if (details[0] === 1) {
const result = prettyPrinter(details[1], 0, seen).replace(/\n+$/u, '');
line += `${ style('Promise', theme.Promise) } { ${ result } }`;
} else if (details[0] === 2) {
line += `${ style('Promise', theme.Promise) } { ${ style('rejected', theme.PromiseRejected) } }`;
}
} else if (typeof value === 'object') {
line += '{';
let keys = Object.getOwnPropertyNames(value);
if (keys.length) {
line += '\n';
}
const enumerables = {};
keys = keys.filter((key) => {
const descriptor = Object.getOwnPropertyDescriptor(value, key);
enumerables[key] = descriptor.enumerable;
return descriptor.enumerable === true || all === true;
});
depth++;
for (let j = 0; j < keys.length; j++) {
const key = keys[j];
const comma = j < keys.length - 1 ? ',' : '';
const keyColor = enumerables[key] ? theme.property : theme.nonEnumerable;
line += `${ prettyPrinter(key, depth, seen, keyColor) }: `;
line += `${ prettyPrinter(value[key], depth, seen) + comma }\n`;
}
depth--;
line += `${ indent(depth) }}`;
} else {
line += style(value.toString(), theme.Unknown);
}
}
return line.replace(/:\s+/gu, ': ').
replace(/([{[])\s+([}\]])/gu, '$1$2');
}
let output = prettyPrinter(object, 0);
if (showDepth) {
output = output.replace(/\n {2}(\s+)/gu, (match, spaces) => `\n ${ spaces.substring(2).split(/ {2}/u).
map(() => style('\u2502 ', theme.decoration)).
join('') }`);
}
if (lineNumbers) {
output = addLineNumbers(output);
}
if (print !== false) {
stream.write(`${ output }\n`);
}
return output;
}
prettyPrint.configure = configure;
prettyPrint.reset = reset;
module.exports = prettyPrint;