Browse Source

Better search for emoji helper

master
Eduard Kuzmenko 3 years ago
parent
commit
ccc6d2c6ef
  1. 2
      src/lib/appManagers/appEmojiManager.ts
  2. 15
      src/lib/searchIndex.ts

2
src/lib/appManagers/appEmojiManager.ts

@ -164,7 +164,7 @@ export class AppEmojiManager { @@ -164,7 +164,7 @@ export class AppEmojiManager {
public indexEmojis() {
if(!this.index) {
this.index = new SearchIndex(false, false);
this.index = new SearchIndex(false, false, 2);
}
for(const langCode in this.keywordLangPacks) {

15
src/lib/searchIndex.ts

@ -14,7 +14,7 @@ import cleanSearchText from '../helpers/cleanSearchText'; @@ -14,7 +14,7 @@ import cleanSearchText from '../helpers/cleanSearchText';
export default class SearchIndex<SearchWhat> {
private fullTexts: Map<SearchWhat, string> = new Map();
constructor(private cleanText = true, private latinize = true) {
constructor(private cleanText = true, private latinize = true, private minChars: number = 1) {
}
@ -57,11 +57,12 @@ export default class SearchIndex<SearchWhat> { @@ -57,11 +57,12 @@ export default class SearchIndex<SearchWhat> {
query = cleanSearchText(query, this.latinize);
}
const newFoundObjs: Array<{fullText: string, what: SearchWhat}> = [];
const newFoundObjs: Array<{fullText: string, fullTextLength: number, what: SearchWhat, foundChars: number}> = [];
const queryWords = query.split(' ');
const queryWordsLength = queryWords.length;
fullTexts.forEach((fullText, what) => {
let found = true;
let foundChars = 0;
for(let i = 0; i < queryWordsLength; ++i) { // * verify that all words are found
const word = queryWords[i];
const idx = fullText.indexOf(word);
@ -69,13 +70,21 @@ export default class SearchIndex<SearchWhat> { @@ -69,13 +70,21 @@ export default class SearchIndex<SearchWhat> {
found = false;
break;
}
foundChars += word.length;
}
if(found) {
newFoundObjs.push({fullText, what});
foundChars += queryWordsLength - 1;
const fullTextLength = fullText.length;
if(this.minChars <= foundChars || fullTextLength <= foundChars) {
newFoundObjs.push({fullText, fullTextLength, what, foundChars});
}
}
});
newFoundObjs.sort((a, b) => a.fullTextLength - b.fullTextLength || b.foundChars - a.foundChars);
//newFoundObjs.sort((a, b) => a.fullText.localeCompare(b.fullText));
const newFoundObjs2: Set<SearchWhat> = new Set(newFoundObjs.map(o => o.what));

Loading…
Cancel
Save