-
Notifications
You must be signed in to change notification settings - Fork 5
/
lbn.js
40 lines (34 loc) · 937 Bytes
/
lbn.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
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
//noinspection JSHint,JSLint
(function (papyrus) {
require('./esc');
var esc = papyrus.esc;
// JSON-like interface for the line-based notation
var LBN = {};
LBN.parse = function (message) {
var stack = esc.svbufParse(message.toString());
var obj = {};
var sv;
//noinspection JSLint
while (sv = stack.svpop()) {
obj[sv.spec] = sv.val;
}
return obj;
};
LBN.stringify = function (obj) {
var keys = [];
var key;
var buf = esc.svbuf();
for (key in obj) {
keys.push(key);
}
keys.sort();
var i;
for (i = 0; i < keys.length; i++) {
buf.svpush(keys[i], obj[keys[i]]);
}
return buf.toString();
};
papyrus.LBN = LBN;
}(g['papyrus'] = g['papyrus'] || {}));