/
index.js
76 lines (63 loc) · 1.68 KB
/
index.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
const util = require('util');
let char, len, params, indent, replacer, removeQuotes, singleLineArray;
function configure(options = {}) {
({
char = '-',
len = 67,
indent = 4,
removeQuotes = true,
singleLineArray = true,
replacer = (key, value) => {
if (singleLineArray && value instanceof Array && typeof value[0] !== 'object')
return JSON.stringify(value);
return value;
},
...params
} = options);
params.breakLength = params.breakLength || len;
}
configure();
function getString(obj, options = {}) {
return util.inspect(obj, {...params, ...options});
}
function log(obj, options) {
console.log(getString(obj, options));
}
function logC(obj, options) {
addLine(log, obj, options);
}
function getJsonString(obj, {
replacer: localReplacer = replacer,
indent: localIndent = indent,
removeQuotes: localRemoveQuotes = removeQuotes,
singleLineArray: localSingleLineArray = singleLineArray
} = {}) {
singleLineArray = localSingleLineArray;
let s = JSON.stringify(obj, localReplacer, localIndent);
if (localRemoveQuotes) {
s = s.replace(/"(\w+)"\s*:/g, '$1:');
}
return s;
}
function json(obj, options) {
console.log(getJsonString(obj, options));
}
function jsonC(obj, options) {
addLine(json, obj, options);
}
function addLine(fn, obj, options) {
line(options);
fn(obj, options);
line(options);
}
function line(options) {
console.log(getLine(options));
}
function getLine({char: localChar = char, len: localLen = len} = {}) {
let s = '';
for (let i = 0; i < localLen; i++) {
s += localChar;
}
return s;
}
module.exports = {configure, getLine, line, getString, log, logC, getJsonString, json, jsonC};