-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (150 loc) · 4.64 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
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
import defaultConfig from './config.js'
const isPrimitive = (test) => {
return test !== Object(test);
}
const isPrimitiveOrEmptyObject = (test) => {
const primative = isPrimitive(test)
if (primative) {
return true
}
if (Object.keys(test).length === 0) {
return true
}
return false
}
const truncate = (str, max, afterTruncateStr) => {
return (str.length > max) ? str.substr(0, max - 1) + afterTruncateStr : str;
};
const reverseString = (str) => {
return str.split("").reverse().join("");
}
const traverse = (obj, depth = 1, options = {}) => {
const {
emptyLinesBefore,
emptyLinesAfter,
offsetOfSpacingChars,
numOfSpacingChars,
spacingChar,
maxLengthBeforeTruncate,
afterTruncateString,
propertyColor,
colonColor,
bracketColor,
booleanColor,
stringColor,
numberColor,
undefinedColor,
nullColor,
functionColor,
symbolColor,
bigintColor
} = options
const parentIsArray = Object.prototype.toString.call(obj) === "[object Array]"
const parentIsPrimativeArray = parentIsArray && obj.every(el => isPrimitive(el) === true)
const spacingString = spacingChar.repeat(depth * numOfSpacingChars + offsetOfSpacingChars)
const closingSpacingString = spacingChar.repeat((depth - 1) * numOfSpacingChars + offsetOfSpacingChars)
let result = ''
if (parentIsPrimativeArray) {
result += bracketColor('[')
} else if (parentIsArray) {
result += bracketColor('[\n')
} else {
result += bracketColor(`${depth === 1 ? closingSpacingString : ''}{\n`)
}
for (const property in obj) {
if (!parentIsArray) {
result += spacingString
result += propertyColor(property)
result += colonColor(':')
result += spacingChar
}
if (parentIsPrimativeArray) {
result += spacingChar
}
const currentPropertyIsPrimativeOrEmptyObject = isPrimitiveOrEmptyObject(obj[property])
if (parentIsArray && !parentIsPrimativeArray && currentPropertyIsPrimativeOrEmptyObject) {
result += spacingString
}
const type = Object.prototype.toString.call(obj[property])
switch (type) {
case '[object Undefined]':
result += undefinedColor(obj[property])
break
case '[object Null]':
result += nullColor(obj[property])
break
case '[object Boolean]':
result += booleanColor(obj[property])
break
case '[object Number]':
result += numberColor(obj[property])
break
case '[object BigInt]':
result += bigintColor(truncate(obj[property].toString(), maxLengthBeforeTruncate, afterTruncateString))
break
case '[object Function]':
result += functionColor(truncate(obj[property].toString(), maxLengthBeforeTruncate, afterTruncateString))
break
case '[object Symbol]':
result += symbolColor(truncate(obj[property].toString(), maxLengthBeforeTruncate, afterTruncateString))
break
case '[object String]':
const displayString = `"${truncate(obj[property], maxLengthBeforeTruncate, afterTruncateString)}"`
result += stringColor(displayString)
break
case '[object Object]': {
if (Object.keys(obj[property]).length === 0) {
result += bracketColor('{}')
break
}
const newDepth = depth + 1
const newResult = traverse(obj[property], newDepth, options)
if (parentIsArray) {
result += spacingString
}
result += newResult
break
}
case '[object Array]': {
const newDepth = depth + 1
const newResult = traverse(obj[property], newDepth, options)
result += newResult
break
}
}
if (parentIsPrimativeArray) {
result += `${bracketColor(',')} `
} else {
result += `${bracketColor(',')}\n`
}
}
if (parentIsPrimativeArray) {
result += `${bracketColor(']')}`
} else if (parentIsArray) {
result += `${closingSpacingString}${bracketColor(']')}`
} else {
result += `${closingSpacingString}${bracketColor('}')}`
}
if(depth === 1){
result = "\n".repeat(emptyLinesBefore) + result + "\n".repeat(emptyLinesAfter)
}
const reversedResult = reverseString(result)
const COMMA_DELIMITER = bracketColor(',')
const reversedKeys = reverseString(COMMA_DELIMITER)
const formatted = reversedResult.replace(reversedKeys, '')
return reverseString(formatted)
}
class JSONChalkify {
constructor(options){
this.options = options
}
chalkify = (obj) => {
const newOptions = {
...defaultConfig,
...this.options
}
const result = traverse(obj, 1, newOptions)
return result
}
}
export default JSONChalkify