forked from l3laze/vdf-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
199 lines (158 loc) · 5.77 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
187
188
189
190
191
192
193
194
195
196
197
198
199
// a simple parser for Valve's KeyValue format
// https://developer.valvesoftware.com/wiki/KeyValues
//
// author: Rossen Popov, 2014-2016
// contributor: Tom Shaver, 2017
// duplicate token can be undefined. If it is, checks are skipped later on.
// it is expected for DUPLICATE_TOKEN to be a string identifier appended to
// duplicate keys
function parse (text, DUPLICATE_TOKEN) {
if (typeof text !== 'string') {
throw new TypeError('VDF.parse: Expecting text parameter to be a string')
}
// If duplicate token exists AND is not a string
if (DUPLICATE_TOKEN && typeof DUPLICATE_TOKEN !== 'string') {
throw new TypeError('VDF.parse: Expecting DUPLICATE_TOKEN parameter to be a string if defined')
}
var lines = text.split('\n')
var obj = {}
var stack = [obj]
var expectBracket = false
var line = ''
var m = ''
var reKV = new RegExp(
'^("((?:\\\\.|[^\\\\"])+)"|([a-z0-9\\-\\_]+))' +
'([ \t]*(' +
'"((?:\\\\.|[^\\\\"])*)(")?' +
'|([a-z0-9\\-\\_]+)' +
'))?'
)
var i = 0
var j = lines.length
for (; i < j; i++) {
line = lines[i].trim()
// skip empty and comment lines
if (line === '' || line[0] === '/') {
continue
}
// todo, implement case for handling #base 'includdes' that will
// import another ENTIRE file to import documents with.
// implemented for now to stop system from erroring out.
if(line[0] === '#' ) {
continue
}
// one level deeper
if (line[0] === '{') {
expectBracket = false
continue
}
if (expectBracket) {
throw new SyntaxError('VDF.parse: expected bracket on line ' + (i + 1))
}
// one level back
if (line[ 0 ] === '}') {
stack.pop()
continue
}
let done = false
// parse keyvalue pairs
while (!done) {
m = reKV.exec(line)
if (m === null) {
throw new SyntaxError('VDF.parse: invalid syntax on line ' + (i + 1))
}
// qkey = 2
// key = 3
// qval = 6
// vq_end = 7
// val = 8
var key = (typeof m[ 2 ] !== 'undefined') ? m[ 2 ] : m[ 3 ]
var val = (typeof m[ 6 ] !== 'undefined') ? m[ 6 ] : m[ 8 ]
if (typeof val === 'undefined') {
// this is a duplicate key so we need to do special increment
// check to see if duplicate token is declared. if it's undefined, the user didn't set it/
// so skip this below operation. instead, proceed to the original behavior of merging.
if(DUPLICATE_TOKEN && stack[stack.length -1][ key ]) {
// if we are in here, the user has opted for not overriding duplicate keys
// we don't know how many duplicate keys exist, so we have to while loop
// and check our increments.
let newKeyFound = false; // by default, no idea where we are
let int = 2; // start at 2, the unmodified first one is "1".
let base = key; // the base of what the key variable should have each time
while(!newKeyFound) {
key = base + `-${DUPLICATE_TOKEN}-${int}`; // what the key shoud look like
// if this key has an assigned value already, keep going up
if( stack[stack.length -1][key] ) {
int++;
continue;
// this key does NOT have anything assigned. Assign it.
} else {
stack[stack.length -1][key] = {} // assign it
newKeyFound = true // break loop
}
}
}
// new key time!
if (!stack[stack.length - 1][ key ]) {
stack[stack.length - 1][ key ] = {}
}
stack.push(stack[stack.length - 1][ key ])
expectBracket = true
} else {
if (!m[ 7 ] && !m[ 8 ]) {
line += '\n' + lines[ ++i ]
continue
}
stack[stack.length - 1][ key ] = val
}
done = true
}
}
if (stack.length !== 1) {
throw new SyntaxError('VDF.parse: open parentheses somewhere')
}
return obj
}
function _dump (obj, pretty, level, DUPLICATE_TOKEN) {
if (typeof obj !== 'object') {
throw new TypeError('VDF.stringify: a key has value of type other than string or object')
}
var indent = '\t'
var buf = ''
var lineIndent = ''
if (pretty) {
for (var i = 0; i < level; i++) {
lineIndent += indent
}
}
for (let key in obj) {
// the key may not be the /binding/ key, for now we declare a variable
// and assign it to key.
// BELOW, with our if statement, we tentatively can change it.
let finalKey = key
// if a duplicate token was defined, check to see if this key has it.
// if it does, override the key in this context with only the original key value by taking index 0
if(DUPLICATE_TOKEN && key.includes(DUPLICATE_TOKEN)) finalKey = key.split(`-${DUPLICATE_TOKEN}-`)[0]
// in the below section, we update finalKey instead of key in this area because
// we want the stripped key as the key. BUT, we want the ORIGINAL keys data.
if (typeof obj[ finalKey ] === 'object') {
buf += [lineIndent, '"', finalKey, '"\n', lineIndent, '{\n', _dump(obj[key], pretty, level + 1, DUPLICATE_TOKEN), lineIndent, '}\n'].join('')
} else {
buf += [lineIndent, '"', finalKey, '"', indent, indent, '"', String(obj[ key ]), '"\n'].join('')
}
}
return buf
}
function stringify (obj, pretty, DUPLICATE_TOKEN) {
if (typeof obj !== 'object') {
throw new TypeError('VDF.stringify: First input parameter is not an object')
}
if(DUPLICATE_TOKEN && typeof DUPLICATE_TOKEN !== 'string') {
throw new TypeError('VDF.stringify: Expecting DUPLICATE_TOKEN parameter to be a string if defined')
}
pretty = (typeof pretty === 'boolean' && pretty)
return _dump(obj, pretty, 0, DUPLICATE_TOKEN)
}
exports.parse = parse
exports.dump = stringify
exports.stringify = stringify