« get me outta code hell

client: filter by kind as postprocessing - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-06-10 20:52:18 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-06-10 20:52:18 -0300
commitef16c1475413c0289f305cde6689a0d176fb8e8c (patch)
tree36e0641cf8f8904958bcf81bd95826a6d7ef6a0c
parent44fbad52edd10b88b996397d5f5456bc1f4e6800 (diff)
client: filter by kind as postprocessing
-rw-r--r--src/static/js/search-worker.js55
1 files changed, 52 insertions, 3 deletions
diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js
index e998e7af..5c7f43f6 100644
--- a/src/static/js/search-worker.js
+++ b/src/static/js/search-worker.js
@@ -409,9 +409,11 @@ function queryGenericIndex(index, query, options) {
   const interestingFields =
     unique(interestingFieldCombinations.flat());
 
-  const terms = query.split(' ');
+  const {genericTerms, queriedKind} =
+    processTerms(query);
 
-  const particles = particulate(terms);
+  const particles =
+    particulate(genericTerms);
 
   const groupedParticles =
     groupArray(particles, ({length}) => length);
@@ -475,7 +477,54 @@ function queryGenericIndex(index, query, options) {
     }
   }
 
-  return boilerplate.constitute(results);
+  const constituted =
+    boilerplate.constitute(results);
+
+  const constitutedAndFiltered =
+    constituted
+      .filter(({id}) =>
+        (queriedKind
+          ? id.split(':')[0] === queriedKind
+          : true));
+
+  return constitutedAndFiltered;
+}
+
+function processTerms(query) {
+  const kindTermSpec = [
+    {kind: 'album', terms: ['album']},
+    {kind: 'artist', terms: ['artist']},
+    {kind: 'flash', terms: ['flash']},
+    {kind: 'group', terms: ['group']},
+    {kind: 'tag', terms: ['art tag', 'tag']},
+    {kind: 'track', terms: ['track']},
+  ];
+
+  const genericTerms = [];
+  let queriedKind = null;
+
+  const termRegexp =
+    new RegExp(
+      String.raw`(?<kind>${kindTermSpec.flatMap(spec => spec.terms).join('|')})` +
+      String.raw`|\S+`,
+      'gi');
+
+  for (const match of query.matchAll(termRegexp)) {
+    const {groups} = match;
+
+    if (groups.kind && !queriedKind) {
+      queriedKind =
+        kindTermSpec
+          .find(({terms}) => terms.includes(groups.kind.toLowerCase()))
+          .kind;
+
+      continue;
+    }
+
+    genericTerms.push(match[0]);
+  }
+
+  return {genericTerms, queriedKind};
 }
 
 function particulate(terms) {