aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/indexsearch.ts
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/src/services/indexsearch.ts')
-rw-r--r--viewer/src/services/indexsearch.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/viewer/src/services/indexsearch.ts b/viewer/src/services/indexsearch.ts
new file mode 100644
index 0000000..a55a829
--- /dev/null
+++ b/viewer/src/services/indexsearch.ts
@@ -0,0 +1,69 @@
1/* ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery.
3--
4-- Copyright (C) 2019-2020 Guillaume FOUET
5--
6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as
8-- published by the Free Software Foundation, either version 3 of the
9-- License, or (at your option) any later version.
10--
11-- This program is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-- GNU Affero General Public License for more details.
15--
16-- You should have received a copy of the GNU Affero General Public License
17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20import { Operation } from "@/@types/Operation";
21
22export default class IndexSearch {
23
24 // Results of the search (by tags)
25 public static search(searchTags: Tag.Search[]): Gallery.Item[] {
26 const byOperation = this.extractTagsByOperation(searchTags);
27 const intersection = this.extractIntersection(byOperation);
28 const substraction = this.extractSubstraction(byOperation);
29 return this.aggregateAll(byOperation, intersection, substraction);
30 }
31
32 private static extractTagsByOperation(searchTags: Tag.Search[]): Tag.SearchByOperation {
33 let byOperation: Tag.SearchByOperation = {};
34 Object.values(Operation).forEach(
35 operation => (byOperation[operation] = searchTags.filter(tag => tag.operation === operation))
36 );
37 return byOperation;
38 }
39
40 private static extractIntersection(byOperation: Tag.SearchByOperation): Set<Gallery.Item> {
41 let intersection = new Set<Gallery.Item>();
42 if (byOperation[Operation.INTERSECTION].length > 0) {
43 byOperation[Operation.INTERSECTION]
44 .map(tag => tag.items)
45 .reduce((a, b) => a.filter(c => b.includes(c)))
46 .flatMap(items => items)
47 .forEach(item => intersection.add(item));
48 }
49 return intersection;
50 }
51
52 private static extractSubstraction(byOperation: Tag.SearchByOperation): Set<Gallery.Item> {
53 let substraction = new Set<Gallery.Item>();
54 if (byOperation[Operation.SUBSTRACTION].length > 0) {
55 byOperation[Operation.SUBSTRACTION].flatMap(tag => tag.items).forEach(item => substraction.add(item));
56 }
57 return substraction;
58 }
59
60 private static aggregateAll(
61 byOperation: Tag.SearchByOperation,
62 intersection: Set<Gallery.Item>,
63 substraction: Set<Gallery.Item>
64 ): Gallery.Item[] {
65 byOperation[Operation.ADDITION].flatMap(tag => tag.items).forEach(item => intersection.add(item));
66 substraction.forEach(item => intersection.delete(item));
67 return [...intersection];
68 }
69}