|
1 | | -// Require Third-party Dependencies |
2 | | -import kleur from "kleur"; |
3 | | -import is from "@slimio/is"; |
| 1 | +// Import Node.js Dependencies |
| 2 | +import { EOL } from "node:os"; |
| 3 | +import { styleText } from "node:util"; |
4 | 4 |
|
| 5 | +// Import Internal Dependencies |
| 6 | +import * as utils from "./src/utils.js"; |
5 | 7 |
|
6 | | -// Require Internal Dependencies |
7 | | -import { maxKeyLength, primeColor } from "./src/utils.js"; |
| 8 | +function logArray(arr, options = {}) { |
| 9 | + const { |
| 10 | + depth = 1, |
| 11 | + disableEndLine = false |
| 12 | + } = options; |
8 | 13 |
|
9 | | -// CONSTANTS |
10 | | -const EOL = "\n"; |
11 | | - |
12 | | -/** |
13 | | - * @function logArray |
14 | | - * @param {!Array<any>} arr |
15 | | - * @param {number} [depth=1] |
16 | | - * @param {boolean} [disableEndLine=false] |
17 | | - * @returns {void} |
18 | | - */ |
19 | | -function logArray(arr, depth = 1, disableEndLine = false) { |
20 | 14 | const backStart = depth === 2 ? " " : " ".repeat(depth - 1); |
21 | 15 | const startSpace = depth === 1 ? " " : " ".repeat(depth); |
22 | 16 | const lenMax = arr.length - 1; |
23 | 17 | const returnLine = arr.length > 10; |
24 | | - const firstIsObject = arr.length > 0 && is.plainObject(arr[0]); |
| 18 | + const firstIsObject = arr.length > 0 && utils.isPlainObject(arr[0]); |
25 | 19 | let forceNext = false; |
26 | 20 |
|
27 | 21 | if (arr.length === 0) { |
28 | | - process.stdout.write(`${kleur.gray("[]")}${disableEndLine ? "" : EOL}`); |
| 22 | + process.stdout.write(`${styleText("gray", "[]")}${disableEndLine ? "" : EOL}`); |
29 | 23 |
|
30 | 24 | return; |
31 | 25 | } |
32 | 26 |
|
33 | | - process.stdout.write(`${kleur.gray("[")}${EOL}${firstIsObject ? "" : startSpace}`); |
| 27 | + process.stdout.write(`${styleText("gray", "[")}${EOL}${firstIsObject ? "" : startSpace}`); |
34 | 28 | for (let id = 0; id < arr.length; id++) { |
35 | 29 | const value = arr[id]; |
36 | 30 |
|
37 | | - if (is.array(value)) { |
| 31 | + if (Array.isArray(value)) { |
38 | 32 | if (id !== 0) { |
39 | 33 | process.stdout.write(startSpace); |
40 | 34 | } |
41 | | - logArray(value, depth + 1, true); |
| 35 | + logArray(value, { depth: depth + 1, disableEndLine: true }); |
42 | 36 | forceNext = true; |
43 | 37 | if (id !== lenMax) { |
44 | | - process.stdout.write(kleur.gray(", ")); |
| 38 | + process.stdout.write(styleText("gray", ", ")); |
45 | 39 | process.stdout.write(EOL); |
46 | 40 | } |
47 | 41 | } |
48 | | - else if (is.plainObject(value)) { |
49 | | - logObject(value, depth, true); |
| 42 | + else if (utils.isPlainObject(value)) { |
| 43 | + logObject(value, { depth, disableEndLine: true }); |
50 | 44 | forceNext = true; |
51 | 45 | } |
52 | 46 | else { |
53 | | - // eslint-disable-next-line |
54 | | - if ((returnLine && id > 0) || forceNext) { |
| 47 | + if ((returnLine && id > 0) || forceNext) { |
55 | 48 | process.stdout.write(startSpace); |
56 | 49 | forceNext = false; |
57 | 50 | } |
58 | 51 |
|
59 | | - process.stdout.write(primeColor(value)(is.string(value) ? `'${value}'` : String(value))); |
| 52 | + const isString = typeof value === "string"; |
| 53 | + process.stdout.write(utils.primeColor(value)(isString ? `'${value}'` : String(value))); |
60 | 54 | if (id !== lenMax) { |
61 | | - process.stdout.write(kleur.gray(", ")); |
| 55 | + process.stdout.write(styleText("gray", ", ")); |
62 | 56 | if (returnLine) { |
63 | 57 | process.stdout.write(EOL); |
64 | 58 | } |
65 | 59 | } |
66 | 60 | } |
67 | 61 | } |
68 | | - process.stdout.write(`${EOL}${backStart}${kleur.gray("]")}${disableEndLine ? "" : EOL}`); |
| 62 | + process.stdout.write(`${EOL}${backStart}${styleText("gray", "]")}${disableEndLine ? "" : EOL}`); |
69 | 63 | } |
70 | 64 |
|
71 | | -/** |
72 | | - * @function logObject |
73 | | - * @param {!object} obj |
74 | | - * @param {number} [depth=1] |
75 | | - * @param {boolean} [disableEndLine=false] |
76 | | - * @returns {void} |
77 | | - */ |
78 | | -function logObject(obj, depth = 1, disableEndLine = false) { |
79 | | - const betweenSpace = maxKeyLength(obj, 4); |
| 65 | +function logObject(obj, options = {}) { |
| 66 | + const { |
| 67 | + depth = 1, |
| 68 | + disableEndLine = false |
| 69 | + } = options; |
| 70 | + const betweenSpace = utils.maxKeyLength(obj, 4); |
80 | 71 | const startSpace = depth === 1 ? " " : " ".repeat(depth); |
81 | 72 | const entries = Object.entries(obj); |
82 | 73 |
|
83 | 74 | if (entries.length === 0) { |
84 | | - process.stdout.write(`${kleur.gray("{}")}${disableEndLine ? "" : EOL}`); |
| 75 | + process.stdout.write(`${styleText("gray", "{}")}${disableEndLine ? "" : EOL}`); |
85 | 76 |
|
86 | 77 | return; |
87 | 78 | } |
88 | 79 |
|
89 | 80 | for (let id = 0; id < entries.length; id++) { |
90 | 81 | const [key, value] = entries[id]; |
91 | | - if (is.func(value) || is.symbol(value)) { |
| 82 | + if (["function", "symbol"].includes(typeof value)) { |
92 | 83 | continue; |
93 | 84 | } |
94 | 85 |
|
95 | 86 | if (id === 0) { |
96 | 87 | process.stdout.write(EOL); |
97 | 88 | } |
98 | | - process.stdout.write(kleur.bold(kleur.white(`${startSpace}${key}: ${" ".repeat(betweenSpace - key.length)}`))); |
99 | | - if (is.object(value)) { |
100 | | - (Array.isArray(value) ? logArray : logObject)(value, depth + 1); |
| 89 | + process.stdout.write(styleText(["bold", "white"], `${startSpace}${key}: ${" ".repeat(betweenSpace - key.length)}`)); |
| 90 | + if (utils.isPlainObject(value)) { |
| 91 | + logObject(value, { depth: depth + 1 }); |
| 92 | + continue; |
| 93 | + } |
| 94 | + if (Array.isArray(value)) { |
| 95 | + logArray(value, { depth: depth + 1 }); |
101 | 96 | continue; |
102 | 97 | } |
103 | 98 |
|
104 | | - process.stdout.write(primeColor(value)(is.string(value) ? `'${value}'` : String(value))); |
| 99 | + process.stdout.write(utils.primeColor(value)(typeof value === "string" ? `'${value}'` : String(value))); |
105 | 100 | if (!disableEndLine) { |
106 | 101 | process.stdout.write(EOL); |
107 | 102 | } |
108 | 103 | } |
109 | 104 | } |
110 | 105 |
|
111 | 106 | export default function stdoutJSON(obj) { |
112 | | - if (is.object(obj)) { |
113 | | - (Array.isArray(obj) ? logArray : logObject)(obj); |
114 | | - process.stdout.write(EOL); |
| 107 | + if (utils.isPlainObject(obj)) { |
| 108 | + logObject(obj); |
| 109 | + } |
| 110 | + else if (Array.isArray(obj)) { |
| 111 | + logArray(obj); |
115 | 112 | } |
116 | 113 | else { |
117 | 114 | throw new Error(`${obj} should be object or array.`); |
118 | 115 | } |
| 116 | + |
| 117 | + process.stdout.write(EOL); |
119 | 118 | } |
120 | 119 |
|
0 commit comments