-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.js
160 lines (143 loc) · 3.6 KB
/
parsing.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
// NOTE Consider moving debugger, debug page, debug page item parsing
// NOTE into separate files at some point.
const DebugPage = require("./DebugPage.js");
const DebugPageItem = require("./DebugPageItem.js");
function PARSING_LOG(message)
{
console.log(`PARSING ${message}`);
}
function debugPageItemToJSON(item)
{
var string = JSON.stringify(item, null, 2);
PARSING_LOG(`debugPageItemToJSON. item: '${string}'`);
return `{"title":"${item.title}","value":"${item.value}","isWritable":${item.isWritable}}`;
}
function debugPageItemsToJSON(items)
{
var itemsJSON = "";
for (var id in items)
{
// Add comma before adding second and following items.
if (itemsJSON.length)
{
itemsJSON += ",";
}
const item = items [id];
const itemJSON = debugPageItemToJSON(item);
itemsJSON += itemJSON;
}
return "[" + itemsJSON + "]";
}
function debugPageToJSON(page)
{
const itemsJSON = debugPageItemsToJSON(page.items);
return `{"title":"${page.title}","items":${itemsJSON}}`;
}
function debugPagesToJSON(pages)
{
var pagesJSON = "";
for (var id in pages)
{
// Add comma before adding second and following items.
if (pagesJSON.length)
{
pagesJSON += ",";
}
const page = pages[id];
const pageJSON = debugPageToJSON(page);
pagesJSON += pageJSON;
}
return "[" + pagesJSON + "]";
}
function debuggerToJSON(title, pages)
{
const pagesJSON = debugPagesToJSON(pages);
return `{"title":"${title}","pages":${pagesJSON}}`
}
function jsonToDebugPageItem(json)
{
if (!json.hasOwnProperty("title"))
{
PARSING_LOG("ERROR Debug page item title is missing");
return null;
}
if (!json.hasOwnProperty("value"))
{
PARSING_LOG("ERROR Debug page item value is missing");
return null;
}
const title = json["title"];
const value = json["value"];
var isWritable = null;
if (json.hasOwnProperty("isWritable"))
{
isWritable = Number(json["isWritable"]);
}
var a = new DebugPageItem(title, value, isWritable);
return a;
}
function jsonToDebugPageItems(json)
{
var items = [];
if (!json.hasOwnProperty("items"))
{
PARSING_LOG("ERROR Debug page items are missing");
return items;
}
for (var id in json["items"])
{
const itemJSON = json["items"][id];
const item = jsonToDebugPageItem(itemJSON);
if (item != null)
{
items.push(item);
}
}
return items;
}
function jsonToDebugPage(json)
{
if (!json.hasOwnProperty("title"))
{
PARSING_LOG("ERROR Debug page title is missing");
return null;
}
const title = json["title"];
const items = jsonToDebugPageItems(json);
return new DebugPage(title, items);
}
function jsonToDebugPages(json)
{
var pages = [];
if (!json.hasOwnProperty("pages"))
{
// NOTE Not really an error if UI simply requests fields to display.
//PARSING_LOG("ERROR Debug pages are missing");
return pages;
}
for (var id in json["pages"])
{
const pageJSON = json["pages"][id];
const page = jsonToDebugPage(pageJSON);
if (page != null)
{
pages.push(page);
}
}
return pages;
}
function jsonToDebuggerTitle(json)
{
if (!json.hasOwnProperty("title"))
{
PARSING_LOG("ERROR Debugger title is missing");
return "";
}
return json["title"];
}
module.exports =
{
debuggerToJSON,
jsonToDebugPages,
jsonToDebuggerTitle,
};