-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (158 loc) · 4.78 KB
/
main.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
/*
* Cette oeuvre, création, site ou texte est sous licence Creative Commons Attribution - Pas de Modification 4.0 International. Pour accéder à une copie de cette licence, merci de vous rendre à l'adresse suivante http://creativecommons.org/licenses/by-nd/4.0/ ou envoyez un courrier à Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/
var fs = require('fs');
//ne pas oublier d'en faire une version async pour permetre d'utiliser caoutchouc dans des callbacks
var Caoutchouc = function (filePath, searchSentence) {
this.tree;
//this.currentLevel;
this.sectionsStartPoint = [];
this.aCursors = {};
if (searchSentence)
this.searchSentence = true;
else
this.searchSentence = false;
if (typeof (filePath) === "array")
{
} else
{
this.tree = JSON.parse(fs.readFileSync(filePath, 'utf8'));
//this.currentLevel = this.tree;
}
this.goThroughObj(this.tree)
}
Caoutchouc.prototype.goThroughObj = function (cur)
{
for (obj in cur)
{
if (typeof (cur[obj]) == "string")
{
if (obj === "_label")
{
this.sectionsStartPoint.push({"name": cur[obj], "level": cur})
}
continue;
} else
{
if (obj.split("|")[0] === "S")
this.sectionsStartPoint.push({"name": obj.split("|")[1], "level": cur[obj]})
}
this.goThroughObj(cur[obj]);
}
}
//| #.*( va| vas| aller| allé).*( sur| à| a).*#
Caoutchouc.prototype.changeToSentenceMatchingRegex = function (key)
{
var aKey = key.split(" ");
var sFinal = ".*(";
var len = aKey.length;
aKey.forEach(function (element, index, array) {
if (index == (len - 1))
sFinal = sFinal + element + ").*";
else
sFinal = sFinal + element + ")*(";
});
}
Caoutchouc.prototype.checkMatchLiteral = function (self, key, searchValue, cursorName)
{
if (searchValue === key)
{
self.aCursors[cursorName] = self.aCursors[cursorName]["L|" + key];
return true
}
}
Caoutchouc.prototype.checkMatchRegex = function (self, key, searchValue, cursorName)
{
if (searchValue.match(key) !== null)
{
self.aCursors[cursorName] = self.aCursors[cursorName]["R|" + key];
return true
}
}
Caoutchouc.prototype.jumpToSection = function (self, key, searchValue, cursorName)
{
}
Caoutchouc.prototype.matchFactory = function (sRef)
{
if (sRef === "_comment")
{
return [false, false];
}
if (sRef === "_jump")
{
return ([true, this.jumpToSection]);
}
if (sRef.charAt(0) === "S")
{
return [false, false];
}
if (sRef.charAt(0) === 'R')
{
return ([true, this.checkMatchRegex])
}
if (sRef.charAt(0) === 'L')
{
return ([true, this.checkMatchLiteral])
}
return [false, false];
}
Caoutchouc.prototype.findLevelInSections = function (sectionName)
{
var i = 0;
while (i < this.sectionsStartPoint.length)
{
if (this.sectionsStartPoint[i].name === sectionName)
{
return this.sectionsStartPoint[i].level;
}
i = i + 1;
}
return null;
}
Caoutchouc.prototype.searchInTree = function (searchValue, cursorName)
{
for (var key in this.aCursors[cursorName]) {
if (this.aCursors[cursorName].hasOwnProperty(key)) {
var tuple = this.matchFactory(key);
if (tuple[0] == true)
{
if (tuple[1](this, key.substr(2), searchValue, cursorName))
{
var response = this.aCursors[cursorName]["_response"];
//jumps the cursor to an other section //need better check for is key exist ?
if (typeof (this.aCursors[cursorName]["_jump"]) !== "undefined")
{
this.aCursors[cursorName] = this.findLevelInSections(this.aCursors[cursorName]["_jump"]);
}
return response;
}
}
}
}
}
//public
Caoutchouc.prototype.showCurrentLevel = function (cursorName)
{
console.log("[Info - level]: ", this.aCursors[cursorName]);
}
//public
Caoutchouc.prototype.createCursor = function (cursorName, sectionName)
{
this.aCursors[cursorName] = this.findLevelInSections(sectionName);
}
//public
Caoutchouc.prototype.deleteCursor = function (cursorName)
{
delete(this.aCursors[cursorName]);
}
//public
Caoutchouc.prototype.moveCursor = function (cursorName, sectionName)
{
this.aCursors[cursorName] = this.findLevelInSections(sectionName);
}
//public
Caoutchouc.prototype.searchInSection = function (cursorName, searchValue)
{
return this.searchInTree(searchValue, cursorName);
}
module.exports = Caoutchouc;