This repository has been archived by the owner on May 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
98 lines (89 loc) · 3.26 KB
/
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
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
const trim = require("trim");
function getStartEnd(string, toFind) {
/*
this part finds the start of the option by searching every possible option name in the string
*/
//for each option toFind
for (let i in toFind) {
if (toFind.hasOwnProperty(i)) {
let option = toFind[i];
//for each name of the option
for (let j = 0; j < option.names.length; j++) {
let name = option.names[j];
//if the name is in the string (the index isn't -1)
let regexp1 = new RegExp(`-${name}( |:)`, "i");
let start = string.search(regexp1);
if (start !== -1) {
/*
this part finds the end of the option by finding the next option
*/
//for each option to find
for (let x in toFind) {
if (toFind.hasOwnProperty(x) && x !== i) { //if the option is not the same as before
let optionEnd = toFind[x];
//for each name of the option
for (let y = 0; y < optionEnd.names.length; y++) {
let nameEnd = optionEnd.names[y];
//if the name is in the string (the index isn't -1)
let regexp2 = new RegExp(`-${nameEnd}( |:)`, "i");
let end = string.search(regexp2);
if (end !== -1) {
//return the start and the end
return {
found: true,
start: start,
end: end,
option: i, // this is the key of the option
name: name //this is the name, not the key of the option object
}
}
}
}
}
//if there isn't a second option, end = end of the string
return {
found: true,
start: start,
end: string.length,
option: i,
name: name
}
}
}
}
}
//if all loops are complete an nothing has been found
//return false
return {
found: false
}
}
function getOptionsArray(string, toFind) {
let workingString = string;
let options = [];
do {
//get the start and the end of the forst option found
var startEnd = getStartEnd(workingString, toFind); //this has to be var (and not let) because of scoping, the while() needs to acces this variable
//take the option from the string
let option = workingString.substring(startEnd.start, startEnd.end);
//push it in the array
options.push({
option: startEnd.option,
name: startEnd.name,
string: option
});
//remove the found option from the string
workingString = workingString.replace(option, "");
} while (startEnd.found);
return options;
}
module.exports = function parse(string, toFind) {
let optionsArray = getOptionsArray(string + " ", toFind); //added " " to avoid bugs with regex that only matches " " or ":", but not word ending
let parsedObj = {};
optionsArray.forEach( (currenOption) => {
let option = currenOption.option === undefined ? "text" : currenOption.option;
let val = option === "text" ? trim(currenOption.string) : (toFind[option].needsValue ? trim(currenOption.string.replace("-" + currenOption.name + ":", "")) : true);
parsedObj[option] = val
});
return parsedObj;
}