-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (168 loc) · 4.15 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
var chalk = require("chalk");
var ansi256 = require("ansi-256-colors");
var themes = require("./themes");
function isFunction(f){
return Object.prototype.toString.call(f) === "[object Function]";
}
function isRegExp(r){
return r instanceof RegExp;
}
function isObject(o){
return !Array.isArray(o) && !isFunction(o) && !isRegExp(o) && o === new Object(o);
}
var reColorName = /^(black|red|green|yellow|blue|magenta|cyan|white|gray)$/;
function coloring(str, color){
str = str + "";
if(typeof color === "function"){
return color(str);
}
else if(reColorName.test(color)){
return chalk[color](str);
}
else if(typeof color === "number" && 0 <= color && color < 256){
return ansi256.fg.codes[color] + str + ansi256.reset;
}
else{
return str;
}
};
var defaultColors = {
"string": "yellow",
"number": "red",
"boolean": "blue",
"infinity": "red",
"nan": "cyan",
"null": "cyan",
"undefined": "gray",
"regexp": "green",
"key": "magenta",
"object": "white",
"array": "white"
};
module.exports = function(json, options){
options = options || {};
if(options.symbol === undefined){
options.symbol = true;
}
options.padding = options.padding || 0;
options.markdown = options.markdown || false;
options.highlight = options.highlight || false;
options.colors = options.colors || {};
if(options.theme){
options.colors = themes[options.theme];
}
Object.keys(defaultColors).forEach(function(key){
options.colors[key] = options.colors[key] || defaultColors[key];
});
var output = "";
var tree = function(o, depth, index, isLast, inArray, type){
index = [].concat(index || []);
depth = depth || 0;
index[depth] = 0;
if(Array.isArray(o)){
if(inArray){
if(options.symbol){
tree("[]", depth, index, isLast, 0, "array");
index[depth] = isLast ? 0 : 1;
}
else{
depth--;
}
}
for(var i = 0, l = o.length;i < l;i++){
tree(o[i], depth + 1, index, i === l - 1, 1);
}
}
else if(isObject(o)){
var keys = Object.keys(o);
if(inArray){
if(options.symbol){
tree("{}", depth, index, isLast, 0, "object");
index[depth] = isLast ? 0 : 1;
depth++;
}
}
for(var i = 0, l = keys.length;i < l;i++){
var lastFlg = (inArray && !isLast && !options.symbol) ? false : (i === l - 1);
tree(keys[i], depth, index, lastFlg, 0, "key");
index[depth] = lastFlg ? 0 : 1;
tree(o[keys[i]], depth + 1, index, 1, 1);
}
}
else{
index[depth] = isLast ? 3 : 2;
if(isFunction(o)){
o = "Function";
type = "function";
}
if(options.markdown){
output += markdownList(depth) + o + "\n";
}
else{
output += format(index) + highlight(o, type) + "\n";
}
}
};
var format = function(index){
var s = "";
var p = new Array(options.padding + 1).join(" ");
for(var i = 0, l = index.length;i < l;i++){
switch(index[i]){
case 1: s += "┃"; break;
case 2: s += "┣ "; break;
case 3: s += "┗ "; break;
default: s += " ";
}
if(i < l - 1){ s += p; }
}
return s;
};
var markdownList = function(depth){
return new Array(depth + 1).join(" ") + "+ ";
};
var highlight = function(data, type){
var hi;
if(!options.highlight){
return data;
}
if(type === "key"){
hi = "key";
}
else if(type === "array"){
hi = "array";
}
else if(type === "object"){
hi = "object";
}
else if(data !== data){
hi = "nan";
}
else if(data === Infinity){
hi = "infinity";
}
else if(typeof data === "string"){
hi = "string";
}
else if(typeof data === "number"){
hi = "number";
}
else if(isRegExp(data)){
hi = "regexp";
}
else if(typeof data === "boolean"){
hi = "boolean";
}
else if(data === null){
hi = "null";
}
else if(data === void 0){
hi = "undefined";
}
else{
return data;
}
return coloring(data, options.colors[hi]);
};
tree(json);
return output;
};