-
Notifications
You must be signed in to change notification settings - Fork 0
/
_parser.js
45 lines (37 loc) · 956 Bytes
/
_parser.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
/** @type {string} */
var s;
console.clear();
var result = [];
var pat1 = /^(?:\d*\.\s).*$/gm;
s.match(pat1).forEach((v, i) => {
var obj = { id: null, question: "", options: [], answer: null, type: null };
let sr = v.replace(/^\d*\.\s/, '');
let sd = sr.match(/\+?\d\)/g);
if (sd == null) {
console.warn(i);
return ;
}
obj.id = v.match(/\d*/)[0];
obj.question = sr.slice(0, sr.indexOf(sd[0]));
let answers = [];
sd.forEach((v, i) => {
if (v[0] == '+') {
answers.push(v[1]);
}
let a;
if (i != sd.length - 1) {
a = sr.slice(sr.indexOf(v) + v.length, sr.indexOf(sd[i + 1]));
} else {
a = sr.slice(sr.indexOf(v) + v.length);
}
obj.options.push(a.trim());
});
obj.type = (answers.length == 1 ? 'radio' : 'select');
obj.answer = answers.map(v => v - 1).join('');
result.push(obj);
});
var r = {
subject_name: "ЭВМ",
catalog: result
}
console.log(JSON.stringify(r));