-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.js
143 lines (142 loc) · 4.43 KB
/
database.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
DB_BASE_PATH = 'data/'
/**
* @constructor
*/
function Database() {
this.db = new Object();
this.search_score = function(haystack, needle, value) {
needle.lastIndex = 0;
var tmp = haystack.match(needle);
if (tmp === null)
return 0;
return tmp.length * value;
};
}
Database.prototype.get_date = function(semestre) {
return this.db[semestre]["DATA"];
}
Database.prototype.set_db = function(semestre, campus) {
if (this.db[semestre] && this.db[semestre][campus]) {
this.cur_db = this.db[semestre][campus];
return 0;
}
return -1;
}
Database.prototype.add = function(semestre, array) {
var self = this;
self.db[semestre] = new Array();
for (var campus in array) {
var campus_array = array[campus];
if (campus === "DATA") {
self.db[semestre][campus] = campus_array;
continue;
}
self.db[semestre][campus] = new Array();
campus_array.forEach(function(k) {
var i = new Object();
i.codigo = k[0];
i.nome_ascii = k[1];
i.nome = k[2];
i.turmas = new Array();
k[3].forEach(function(m) {
var n = new Object();
n.nome = m[0];
n.horas_aula = m[1];
n.vagas_ofertadas = m[2];
n.vagas_ocupadas = m[3];
n.alunos_especiais = m[4];
n.saldo_vagas = m[5];
n.pedidos_sem_vaga = m[6];
n.horarios = m[7];
n.professores = m[8];
i.turmas.push(n);
});
self.db[semestre][campus][i.codigo] = i;
self.db[semestre][campus].push(i);
});
}
}
Database.prototype.fetch = function(string, page) {
string = string.toUpperCase()
.replace(/[ÀÁÂÃÄÅ]/g, "A")
.replace(/[ÈÉÊË]/g, "E")
.replace(/[ÌÍÎÏ]/g, "I")
.replace(/[ÒÓÔÕÖØ]/g, "O")
.replace(/[ÙÚÛÜ]/g, "U")
.replace(/Ç/g, "C")
.replace(/Ð/g, "D")
.replace(/Ñ/g, "N")
.replace(/Ý/g, "Y")
.replace(/ß/g, "B");
var search_whole = [];
var search_part = [];
var needles = string.split(" ");
needles.forEach(function(str) {
if (str != "") {
search_whole.push(new RegExp("\\b" + str + "\\b", "g"));
search_part.push(new RegExp(str, "g"));
}
});
this.result = [];
this.result.forEach(function(t) {
delete t.score;
});
for (var i = 0; i < this.cur_db.length; i++) {
var haystack = this.cur_db[i];
var firstword = haystack.nome_ascii.split(" ")[0];
var exactly = false;
var score = 0;
for (var j = 0; j < search_whole.length; j++) {
var expr_score = 0;
search_whole[j].lastIndex = 0;
if (search_whole[j].test(haystack.codigo)) {
exactly = true;
continue;
}
if (firstword == needles[j])
expr_score += 200;
expr_score += this.search_score(haystack.nome_ascii, search_whole[j], 100);
expr_score += this.search_score(haystack.nome_ascii, search_part[j], 10);
expr_score += this.search_score(haystack.codigo, search_part[j], 1);
if (expr_score) {
score += expr_score;
} else {
score = 0;
break;
}
}
if (exactly) {
this.result = [haystack];
break;
}
if (score) {
haystack.score = score;
this.result.push(haystack);
}
}
this.result.sort(function(a,b) {
var diff = b.score - a.score;
if (!diff) {
if (a.score < 10 && b.score < 10) {
if (b.codigo < a.codigo) {
diff = 1;
} else if (a.codigo < b.codigo) {
diff = -1;
}
} else {
if (b.nome_ascii < a.nome_ascii) {
diff = 1;
} else if (a.nome_ascii < b.nome_ascii) {
diff = -1;
}
}
}
return diff;
});
}
Database.prototype.page = function(page) {
return this.result.slice(page*10, (page+1)*10);
}
Database.prototype.full = function(string) {
return this.cur_db[string];
}