-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_searcher.js
executable file
·71 lines (64 loc) · 2.02 KB
/
array_searcher.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
export default class arraySearcher{
constructor(maxHashLength = 1){
this._index = {};
this._arr = {};
this._relation = null;
this.maxHashLength = maxHashLength;
}
setArray(arr){
this._relation = null;
this._index = {};
this._arr = arr;
this._makeHash();
}
setHash(objArray, valueKeys){
this._relation = {};
this._index = {};
this._arr = [];
objArray.forEach( item => {
let value = item;
valueKeys.forEach( valueKey => { value = value[valueKey] });
this._relation[value] = { item: item, value: value };
this._arr.push(value);
});
this._makeHash();
}
_makeHash(){
this._arr.forEach( item => {
for( var i=1; i<=this.maxHashLength; i++){
let hashstr = item.substr(0, i);
if( hashstr === "" ) break;
if( this._index[hashstr] ){
this._index[hashstr].push(item);
}
else{
this._index[hashstr] = [item];
}
}
});
}
_getObj(arrItems){
if( !arrItems ) return [];
if( this._relation ){
return arrItems.map( arrItem => this._relation[arrItem] );
}
else{
return arrItems;
}
}
search(keyword){
let results = [];
if( keyword.length <= this.maxHashLength ){
results = this._getObj(this._index[keyword]);
}
else{
let keywordHash = keyword.substr(0, this.maxHashLength);
let objs = this._getObj(this._index[keywordHash]);
objs.forEach( obj => {
let val = (typeof obj !== "string") ? obj.value : obj;
if( val.includes(keyword) ) results.push(obj)
} );
}
return this._relation ? results.map( obj => obj.item ) : results;
}
}