-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindent.json.js
236 lines (204 loc) · 6.35 KB
/
indent.json.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const IGNORED = [
'{','}',
'[',']',
]
function splitKeyValue(line) {
let inQuotes = false;
let currentQuote = '';
let escapeNext = false;
let colonIndex = -1;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (escapeNext) {
escapeNext = false;
continue;
}
if (char === '\\') {
escapeNext = true;
continue;
}
if (char === '"' || char === "'") {
if (!inQuotes) {
inQuotes = true;
currentQuote = char;
} else if (currentQuote === char) {
inQuotes = false;
currentQuote = '';
}
}
if (char === ':' && !inQuotes) {
colonIndex = i;
break;
}
}
if (colonIndex === -1) {
return { key: '', value: line };
}
const key = line.slice(0, colonIndex);
const value = line.slice(colonIndex + 1);
return { key, value };
}
function splitCommaDelimited(line) {
let inQuotes = false;
let currentQuote = '';
let escapeNext = false;
let lastComma = -1;
let values = [];
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (escapeNext) {
escapeNext = false;
continue;
}
if (char === '\\') {
escapeNext = true;
continue;
}
if (char === '"' || char === "'") {
if (!inQuotes) {
inQuotes = true;
currentQuote = char;
} else if (currentQuote === char) {
inQuotes = false;
currentQuote = '';
}
}
if (char === ',' && !inQuotes) {
values.push(line.slice(lastComma + 1, i));
lastComma = i;
}
}
values.push(line.slice(lastComma + 1));
return values;
}
function alignJSON( obj, arrays = false, maxlength = 80 ){
var json = JSON.stringify( obj, null, 4 );
// get the longest line
var lines = json.split('\n');
var longest = lines
.filter( line => line.trim().length > 0 )
.filter( line => !IGNORED.some( char => line.includes(char) ) )
.filter( line => {
let { value } = splitKeyValue(line);
return value.trim().length > 0;
})
.map( line => {
let { key } = splitKeyValue(line);
return key
})
.reduce( (a,b) => {
return a.length > b.length ? a : b
});
var minimum_value_start = longest.length + 2;
lines = lines.map((
line,
index,
lines
) => {
if( line.trim().length === 0 )
return line;
// check if line has ignored characters
var ignore = IGNORED.some( char => line.includes(char) );
if( ignore )
return line;
let {
key,
value
} = splitKeyValue(line);
if( key.trim().length ){
value = value.trim();
const separator = ': ';
const padding = minimum_value_start - key.length - separator.length;
if( padding > 0 )
return key + separator + ' '.repeat(padding) + value;
else
return key + separator + value;
} else if( arrays ) {
value = value.trim();
return ' '.repeat(minimum_value_start) + value;
} else {
const firstvalue = value;
// get the rest of the array
let values = [ firstvalue ];
while( true ){
let next = lines[index + values.length];
if( !next )
break;
if( next.trim().length === 0 )
continue;
if( IGNORED.some( char => next.includes(char) ) )
break;
let { key, value } = splitKeyValue(next);
if( key.trim().length )
break;
values.push( value.trim() );
}
lines.splice( index + 1, values.length - 1 );
return values.join(' ');
}
});
const longest_nonarray_line = lines
.filter( line => !IGNORED.some( char => line.includes(char) ) )
.filter( line => {
let { key } = splitKeyValue(line);
return key.trim().length > 0;
})
.reduce( (a,b) => {
return a.length > b.length ? a : b
});
const max_linelength = Math.min( maxlength, longest_nonarray_line.length );
// break up long array lines (comma delimited)
lines = lines.map((
line,
index,
)=>{
if( line.trim().length === 0 )
return line;
// check if line has ignored characters
var ignore = IGNORED.some( char => line.includes(char) );
if( ignore )
return line;
let {
key,
value
} = splitKeyValue(line);
if( key.trim().length )
return line;
else {
// get the rest of the array
let values = splitCommaDelimited(value);
if( values.length === 1 )
return line;
let firstvalue = values[0];
const padding = firstvalue.length - firstvalue.trim().length;
const separator = ', ';
let newlines = [];
while( true ){
const newline = [ values.shift().trim() ];
while( values.length ){
let next = values.shift();
let trimmed = next.trim();
const currentlinelength = (' '.repeat( padding ) + newline.join(separator)).length;
if( currentlinelength + trimmed.length + separator.length < max_linelength ){
newline.push( trimmed );
} else {
values.unshift( next );
break;
}
}
newlines.push( ' '.repeat( padding ) + newline.join(separator) );
if( values.length === 0 )
break;
}
return newlines.join(',\n');
}
})
return lines
.filter( line => line.trim().length > 0 )
.join('\n');
}
module.exports = {
alignJSON,
splitKeyValue,
splitCommaDelimited,
}