forked from xavi-/node-properties-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
350 lines (280 loc) · 8.7 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var fs = require("fs");
function Iterator(text) {
var pos = 0, length = text.length;
this.peek = function(num) {
num = num || 0;
if(pos + num >= length) { return null; }
return text.charAt(pos + num);
};
this.next = function(inc) {
inc = inc || 1;
if(pos >= length) { return null; }
return text.charAt((pos += inc) - inc);
};
this.pos = function() {
return pos;
};
}
var rWhitespace = /\s/;
function isWhitespace(chr) {
return rWhitespace.test(chr);
}
function consumeWhiteSpace(iter) {
var start = iter.pos();
while(isWhitespace(iter.peek())) { iter.next(); }
return { type: "whitespace", start: start, end: iter.pos() };
}
function startsComment(chr) {
return chr === "!" || chr === "#";
}
function isEOL(chr) {
return chr == null || chr === "\n" || chr === "\r";
}
function consumeComment(iter) {
var start = iter.pos();
while(!isEOL(iter.peek())) { iter.next(); }
return { type: "comment", start: start, end: iter.pos() };
}
function startsKeyVal(chr) {
return !isWhitespace(chr) && !startsComment(chr);
}
function startsSeparator(chr) {
return chr === "=" || chr === ":" || isWhitespace(chr);
}
function startsEscapedVal(chr) {
return chr === "\\";
}
function consumeEscapedVal(iter) {
var start = iter.pos();
iter.next(); // move past "\"
var curChar = iter.next();
if(curChar === "u") { // encoded unicode char
iter.next(4); // Read in the 4 hex values
}
return { type: "escaped-value", start: start, end: iter.pos() };
}
function consumeKey(iter) {
var start = iter.pos(), children = [];
var curChar;
while((curChar = iter.peek()) !== null) {
if(startsSeparator(curChar)) { break; }
if(startsEscapedVal(curChar)) { children.push(consumeEscapedVal(iter)); continue; }
iter.next();
}
return { type: "key", start: start, end: iter.pos(), children: children };
}
function consumeKeyValSeparator(iter) {
var start = iter.pos();
var seenHardSep = false, curChar;
while((curChar = iter.peek()) !== null) {
if(isEOL(curChar)) { break; }
if(isWhitespace(curChar)) { iter.next(); continue; }
if(seenHardSep) { break; }
seenHardSep = (curChar === ":" || curChar === "=");
if(seenHardSep) { iter.next(); continue; }
break; // curChar is a non-separtor char
}
return { type: "key-value-separator", start: start, end: iter.pos() };
}
function startsLineBreak(iter) {
return iter.peek() === "\\" && isEOL(iter.peek(1));
}
function consumeLineBreak(iter) {
var start = iter.pos();
iter.next(); // consume \
if(iter.peek() === "\r") { iter.next(); }
iter.next(); // consume \n
var curChar;
while((curChar = iter.peek()) !== null) {
if(isEOL(curChar)) { break; }
if(!isWhitespace(curChar)) { break; }
iter.next();
}
return { type: "line-break", start: start, end: iter.pos() };
}
function consumeVal(iter) {
var start = iter.pos(), children = [];
var curChar;
while((curChar = iter.peek()) !== null) {
if(startsLineBreak(iter)) { children.push(consumeLineBreak(iter)); continue; }
if(startsEscapedVal(curChar)) { children.push(consumeEscapedVal(iter)); continue; }
if(isEOL(curChar)) { break; }
iter.next();
}
return { type: "value", start: start, end: iter.pos(), children: children };
}
function consumeKeyVal(iter) {
return {
type: "key-value",
start: iter.pos(),
children: [
consumeKey(iter),
consumeKeyValSeparator(iter),
consumeVal(iter)
],
end: iter.pos()
};
}
var renderChild = {
"escaped-value": function(child, text) {
var type = text.charAt(child.start + 1);
if(type === "t") { return "\t"; }
if(type === "r") { return "\r"; }
if(type === "n") { return "\n"; }
if(type === "f") { return "\f"; }
if(type !== "u") { return type; }
return String.fromCharCode(parseInt(text.substr(child.start + 2, 4), 16));
},
"line-break": function (child, text) {
return "";
}
};
function rangeToBuffer(range, text) {
var start = range.start, buffer = [];
for(var i = 0; i < range.children.length; i++) {
var child = range.children[i];
buffer.push(text.substring(start, child.start));
buffer.push(renderChild[child.type](child, text));
start = child.end;
}
buffer.push(text.substring(start, range.end));
return buffer;
}
function rangesToObject(ranges, text) {
var obj = Object.create(null); // Creates to a true hash map
for(var i = 0; i < ranges.length; i++) {
var range = ranges[i];
if(range.type !== "key-value") { continue; }
var key = rangeToBuffer(range.children[0], text).join("");
var val = rangeToBuffer(range.children[2], text).join("");
obj[key] = val;
}
return obj;
}
function stringToRanges(text) {
var iter = new Iterator(text), ranges = [];
var curChar;
while((curChar = iter.peek()) !== null) {
if(isWhitespace(curChar)) { ranges.push(consumeWhiteSpace(iter)); continue; }
if(startsComment(curChar)) { ranges.push(consumeComment(iter)); continue; }
if(startsKeyVal(curChar)) { ranges.push(consumeKeyVal(iter)); continue; }
throw Error("Something crazy happened. text: '" + text + "'; curChar: '" + curChar + "'");
}
return ranges;
}
function Editor(text, path) {
text = text || "";
// Make sure there's an empty line at the end
// otherwise the first new property will be placed at the end of the last line from the original text
if (text.length > 0 && text[text.length-1] != '\n'){
text += "\n";
}
var ranges = stringToRanges(text);
var obj = rangesToObject(ranges, text);
var keyRange = Object.create(null); // Creates to a true hash map
for(var i = 0; i < ranges.length; i++) {
var range = ranges[i];
if(range.type !== "key-value") { continue; }
var key = rangeToBuffer(range.children[0], text).join("");
keyRange[key] = range;
}
this.addHeadComment = function(comment) {
if(comment == null) { return; }
ranges.unshift({ type: "literal", text: "# " + comment.replace(/\n/g, "\n# ") });
};
this.get = function(key) { return obj[key]; };
this.set = function(key, val, comment) {
if(val == null) { this.unset(key); return; }
obj[key] = val;
var range = keyRange[key];
if(!range) {
keyRange[key] = range = { type: "literal", text: key + "=" + val };
ranges.push(range);
}
// comment === null deletes comment. if comment === undefined, it's left alone
if(comment !== undefined) {
range.comment = comment && "# " + comment.replace(/\n/g, "\n# ");
}
if(range.type === "literal") {
range.text = key + "=" + val;
if(range.comment != null) { range.text = range.comment + "\n" + range.text; }
} else if(range.type === "key-value") {
range.children[2].type = "literal";
range.children[2].text = val;
range.children[2].parent = range;
} else {
throw "Unknown node type: " + range.type;
}
};
this.unset = function(key) {
var range = keyRange[key];
var index = ranges.indexOf(range);
if (index > -1) {
// remove any trailing whitespace
var numToRemove = 1;
while (index + numToRemove < ranges.length && ranges[index + numToRemove].type === "whitespace" )
numToRemove ++;
ranges.splice(index, numToRemove);
}
delete keyRange[key];
delete obj[key];
};
this.valueOf = this.toString = function() {
var buffer = [], stack = [].concat(ranges);
var node;
while((node = stack.shift()) != null) {
switch(node.type) {
case "literal":
buffer.push(node.text);
if(!node.parent) { buffer.push("\n"); }
else { buffer.push(text.substring(node.end, node.parent.end)); }
break;
case "key":
case "value":
case "comment":
case "whitespace":
case "key-value-separator":
case "escaped-value":
case "line-break":
buffer.push(text.substring(node.start, node.end));
break;
case "key-value":
Array.prototype.unshift.apply(stack, node.children);
if(node.comment) { stack.unshift({ type: "literal", text: node.comment }); }
break;
}
}
return buffer.join("");
};
this.save = function(newPath, callback) {
if(typeof newPath === 'function') {
callback = newPath;
newPath = path;
}
newPath = newPath || path;
if(!newPath) { callback("Unknown path"); }
fs.writeFile(newPath, this.toString(), callback || function() {});
};
}
function createEditor(path, callback) {
if(!path) { return new Editor(); }
if(!callback) { return new Editor(fs.readFileSync(path).toString(), path); }
return fs.readFile(path, function(err, text) {
if(err) { return callback(err, null); }
text = text.toString();
return callback(null, new Editor(text, path));
});
}
function parse(text) {
text = text.toString();
var ranges = stringToRanges(text);
return rangesToObject(ranges, text);
}
function read(path, callback) {
if(!callback) { return parse(fs.readFileSync(path)); }
return fs.readFile(path, function(err, data) {
if(err) { return callback(err, null); }
return callback(null, parse(data));
});
}
module.exports = { parse: parse, read: read, createEditor: createEditor };