forgejo-notifications/src/routes/search.js
2023-09-19 22:03:07 +05:30

50 lines
1,003 B
JavaScript

// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
export class Item {
constructor(id, content, itemType) {
this.id = id;
this.content = content;
this.itemType = itemType;
}
isType(itemType) {
if (this.itemType.find((i) => i == itemType)) {
return true;
} else {
return false;
}
}
match(query) {
return this.content.includes(query);
}
}
export class Index {
constructor() {
this.items = [];
}
index(id, content, itemType) {
this.items.push(new Item(id, content, itemType));
}
typedSearch(itemType, query) {
let filtered = this.items.filter((indexItem) =>
itemType.find((it) => indexItem.isType(it))
);
return this.innerSearch(filtered, query);
}
search(query) {
return this.innerSearch(this.items, query);
}
innerSearch(items, query) {
let matches = items.filter((i) => i.match(query));
return matches;
}
}