-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (111 loc) · 3.5 KB
/
index.js
File metadata and controls
129 lines (111 loc) · 3.5 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
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
// Import Node.js Dependencies
import { EOL } from "node:os";
import { styleText } from "node:util";
// Import Internal Dependencies
import * as utils from "./src/utils.js";
function logArray(arr, options = {}) {
const {
depth = 1,
disableEndLine = false
} = options;
const backStart = depth === 2 ? " " : " ".repeat(depth - 1);
const startSpace = depth === 1 ? " " : " ".repeat(depth);
const lenMax = arr.length - 1;
const returnLine = utils.shouldArrayReturnLine(arr, { depth });
const firstIsObject = arr.length > 0 && utils.isPlainObject(arr[0]);
let forceNext = false;
if (arr.length === 0) {
process.stdout.write(`${styleText("gray", "[]")}${disableEndLine ? "" : EOL}`);
return;
}
if (depth === 1) {
process.stdout.write(EOL);
}
const shouldReturn = utils.isPlainObject(arr[0]) === false;
process.stdout.write(`${styleText("gray", "[")}${shouldReturn ? EOL : ""}${firstIsObject ? "" : startSpace}`);
for (let id = 0; id < arr.length; id++) {
const value = arr[id];
if (Array.isArray(value)) {
if (id !== 0) {
process.stdout.write(startSpace);
}
logArray(value, { depth: depth + 1, disableEndLine: true });
forceNext = true;
if (id !== lenMax) {
process.stdout.write(styleText("gray", ", "));
process.stdout.write(EOL);
}
}
else if (utils.isPlainObject(value)) {
logObject(value, {
depth,
disableEndLine: utils.shouldObjectReturnLine(value, { depth }),
inArray: true
});
forceNext = true;
}
else {
if ((returnLine && id > 0) || forceNext) {
process.stdout.write(startSpace);
forceNext = false;
}
const isString = typeof value === "string";
process.stdout.write(utils.primeColor(value)(isString ? `'${value}'` : String(value)));
if (id !== lenMax) {
process.stdout.write(styleText("gray", ", "));
if (returnLine) {
process.stdout.write(EOL);
}
}
}
}
process.stdout.write(`${EOL}${backStart}${styleText("gray", "]")}${disableEndLine ? "" : EOL}`);
}
function logObject(obj, options = {}) {
const {
depth = 1,
disableEndLine = false,
inArray = false
} = options;
const betweenSpace = utils.maxKeyLength(obj, 4);
const startSpace = depth === 1 ? " " : " ".repeat(depth);
const entries = Object.entries(obj);
if (entries.length === 0) {
process.stdout.write(`${styleText("gray", "{}")}${disableEndLine ? "" : EOL}`);
return;
}
for (let id = 0; id < entries.length; id++) {
const [key, value] = entries[id];
if (["function", "symbol"].includes(typeof value)) {
continue;
}
if (id === 0) {
process.stdout.write(EOL);
}
process.stdout.write(styleText(["bold", "white"], `${startSpace}${key}: ${" ".repeat(betweenSpace - key.length)}`));
if (utils.isPlainObject(value)) {
logObject(value, { depth: depth + 1 });
continue;
}
if (Array.isArray(value)) {
logArray(value, { depth: depth + 1 });
continue;
}
process.stdout.write(utils.primeColor(value)(typeof value === "string" ? `'${value}'` : String(value)));
if (!disableEndLine && !(inArray && id === entries.length - 1)) {
process.stdout.write(EOL);
}
}
}
export default function stdoutJSON(obj) {
if (utils.isPlainObject(obj)) {
logObject(obj);
}
else if (Array.isArray(obj)) {
logArray(obj);
}
else {
throw new Error(`${obj} should be object or array.`);
}
process.stdout.write(EOL);
}