-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexer.js
74 lines (47 loc) · 1.29 KB
/
Indexer.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
const crypto = require('crypto');
module.exports = class Indexer {
hashCode(str, algo = "SHA-256") {
return crypto.createHash('sha1').update(str).digest('hex');
}
filterStr(str) {
return str.replaceAll(",", "").replaceAll("(", "").replaceAll(")", "");
}
setIndex(key, field, index, id) {
this.db.setIndex(key, field, index, id, this.hashCode(this.filterStr(key)));
}
* splitStrIterator(str) {
let i = -1, character, partialString = "";
while (true) {
i++;
character = str[i];
if (character === undefined) {
if (partialString.length > 0) yield this.filterStr(partialString);
break;
}
if (["-", " "].includes(character)) {
yield this.filterStr(partialString);
partialString = "";
continue;
}
partialString += character;
}
}
index(mocks) {
for (const mock of mocks) {
const indexableKeys = ['desc'];
for (const key of indexableKeys) {
const field = mock[key];
let index = 0;
for (const str of this.splitStrIterator(field.toLowerCase())) {
// console.log(str, index);
this.setIndex(str, key, index, mock.id);
index++;
}
index = 0;
}
}
}
setDb(db) {
this.db = db;
}
};