analyze.js
2.37 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
var tokenize = require('./parse').tokenize
module.exports.analyze = function analyzeJSON(input, options) {
if (options == null) options = {}
if (!Array.isArray(input)) {
input = tokenize(input, options)
}
var result = {
has_whitespace: false,
has_comments: false,
has_newlines: false,
has_trailing_comma: false,
indent: '',
newline: '\n',
quote: '"',
quote_keys: true,
}
var stats = {
indent: {},
newline: {},
quote: {},
}
for (var i=0; i<input.length; i++) {
if (input[i].type === 'newline') {
if (input[i+1] && input[i+1].type === 'whitespace') {
if (input[i+1].raw[0] === '\t') {
// if first is tab, then indent is tab
stats.indent['\t'] = (stats.indent['\t'] || 0) + 1
}
if (input[i+1].raw.match(/^\x20+$/)) {
// if all are spaces, then indent is space
// this can fail with mixed indent (4, 2 would display 3)
var ws_len = input[i+1].raw.length
var indent_len = input[i+1].stack.length + 1
if (ws_len % indent_len === 0) {
var t = Array(ws_len / indent_len + 1).join(' ')
stats.indent[t] = (stats.indent[t] || 0) + 1
}
}
}
stats.newline[input[i].raw] = (stats.newline[input[i].raw] || 0) + 1
}
if (input[i].type === 'newline') {
result.has_newlines = true
}
if (input[i].type === 'whitespace') {
result.has_whitespace = true
}
if (input[i].type === 'comment') {
result.has_comments = true
}
if (input[i].type === 'key') {
if (input[i].raw[0] !== '"' && input[i].raw[0] !== "'") result.quote_keys = false
}
if (input[i].type === 'key' || input[i].type === 'literal') {
if (input[i].raw[0] === '"' || input[i].raw[0] === "'") {
stats.quote[input[i].raw[0]] = (stats.quote[input[i].raw[0]] || 0) + 1
}
}
if (input[i].type === 'separator' && input[i].raw === ',') {
for (var j=i+1; j<input.length; j++) {
if (input[j].type === 'literal' || input[j].type === 'key') break
if (input[j].type === 'separator') result.has_trailing_comma = true
}
}
}
for (var k in stats) {
if (Object.keys(stats[k]).length) {
result[k] = Object.keys(stats[k]).reduce(function(a, b) {
return stats[k][a] > stats[k][b] ? a : b
})
}
}
return result
}