-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonc.js
executable file
·68 lines (64 loc) · 1.69 KB
/
jsonc.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
// removes comments from JSON without changing the line numbers
module.exports = function() {
var self = {
init: function() {
},
parse: function(txt) {
const states = {
TEXT: 'text',
SINGLE_QUOTED: 'single quoted',
DOUBLE_QUOTED: 'double quoted',
LINE_COMMENT: 'line comment',
BLOCK_COMMENT: 'block comment'
}
let res = ''
let state = states.TEXT
let parts = txt.split(/("|'|\n|\/\/|\/\*|\*\/|\\"|\\|')/)
for(let i=0; i<parts.length; i++) {
let c = parts[i]
switch(state) {
case states.TEXT:
switch(c) {
case '"': state = states.DOUBLE_QUOTED; res += c; break
case '\'': state = states.SINGLE_QUOTED; res += '"'; break
case '//': state = states.LINE_COMMENT; break
case '/*': state = states.BLOCK_COMMENT; break
default: res += c
}
break
case states.SINGLE_QUOTED:
if (c == "'") {
state = states.TEXT
res += '"'
} else if (c == '"') {
res += '\\"'
} else {
res += c
}
break
case states.DOUBLE_QUOTED:
if (c == '"') state = states.TEXT
res += c
break
case states.LINE_COMMENT:
if (c == '\n') {
state = states.TEXT
res += c
}
break
case states.BLOCK_COMMENT:
if (c == '*/') {
state = states.TEXT
}
break
}
}
// but remove trailing comma in arrays/objects
// TODO currently also removes inside strings
res = res.replace(/,(\s*)(}|])/g, '$1$2')
return JSON.parse(res)
}
}
self.init()
return self
}