« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/data/things/search.js75
-rw-r--r--src/static/clientSearch.js37
-rwxr-xr-xsrc/upd8.js9
3 files changed, 59 insertions, 62 deletions
diff --git a/src/data/things/search.js b/src/data/things/search.js
index df177071..ad641c38 100644
--- a/src/data/things/search.js
+++ b/src/data/things/search.js
@@ -2,21 +2,14 @@
 
 'use strict';
 
-import {
-  writeFile,
-} from 'node:fs/promises';
+import {writeFile} from 'node:fs/promises';
 
-import {
-  logWarn,
-  logInfo,
-  logError,
-} from '#cli';
+import FlexSearch from 'flexsearch';
 
+import {logError, logInfo, logWarn} from '#cli';
 import Thing from '#thing';
 
-import FlexSearch from 'flexsearch';
-
-export async function writeSearchIndex(search_index_path, wikiData) {
+export async function writeSearchIndex(searchIndexPath, wikiData) {
 
   // Basic flow is:
   // 1. Define schema for type
@@ -29,62 +22,64 @@ export async function writeSearchIndex(search_index_path, wikiData) {
       id: "reference",
       index: ["name", "groups"],
     }),
+
     tracks: new FlexSearch.Document({
       id: "reference",
       index: ["track", "album", "artists", "directory", "additionalNames"],
     }),
+
     artists: new FlexSearch.Document({
       id: "reference",
       index: ["names"],
-    })
-  }
+    }),
+  };
 
-  wikiData.albumData.forEach((album) => {
+  for (const album of wikiData.albumData) {
     indexes.albums.add({
       reference: Thing.getReference(album),
       name: album.name,
       groups: album.groups.map(group => group.name),
-    })
+    });
 
-    album.tracks.forEach((track) => {
+    for (const track of album.tracks) {
       indexes.tracks.add({
         reference: Thing.getReference(track),
         album: album.name,
         track: track.name,
+
         artists: [
           track.artistContribs.map(contrib => contrib.artist.name),
           ...track.artistContribs.map(contrib => contrib.artist.aliasNames)
         ],
-        additionalNames: track.additionalNames.map(entry => entry.name)
-      })
-    })
-  });
-
-  wikiData.artistData
-  .filter(artist => !artist.isAlias)
-  .forEach((artist) => {
+
+        additionalNames:
+          track.additionalNames.map(entry => entry.name),
+      });
+    }
+  }
+
+  for (const artist of wikiData.artistData) {
+    if (artist.isAlias) {
+      continue;
+    }
+
     indexes.artists.add({
       reference: Thing.getReference(artist),
-      names: [
-        artist.name,
-        ...artist.aliasNames
-      ]
-    })
-  })
+      names: [artist.name, ...artist.aliasNames],
+    });
+  }
 
   // Export indexes to json
-  let searchData = {}
+  const searchData = {}
 
   await Promise.all(
     Object.entries(indexes)
-    .map(pair => {
-      const [index_name, index] = pair
-      searchData[index_name] = {}
-      return index.export((key, data) => {
-        searchData[index_name][key] = data
-      });
-    })
-  )
+      .map(([indexName, index]) => {
+        searchData[indexName] = {};
+        return index.export((key, data) => {
+          searchData[indexName][key] = data
+        });
+      }));
 
-  writeFile(search_index_path, JSON.stringify(searchData))
+  await writeFile(searchIndexPath, JSON.stringify(searchData));
 }
diff --git a/src/static/clientSearch.js b/src/static/clientSearch.js
index 4d01cfd9..13a34528 100644
--- a/src/static/clientSearch.js
+++ b/src/static/clientSearch.js
@@ -1,7 +1,7 @@
 /* eslint-env browser */
 
 async function initSearch() {
-  const FlexSearch = window.FlexSearch;
+  const {FlexSearch} = window;
 
   // Copied directly from server search.js
   window.indexes = {
@@ -9,34 +9,37 @@ async function initSearch() {
       id: "reference",
       index: ["name", "groups"],
     }),
+
     tracks: new FlexSearch.Document({
       id: "reference",
       index: ["track", "album", "artists", "directory", "additionalNames"],
     }),
+
     artists: new FlexSearch.Document({
       id: "reference",
       index: ["names"],
-    })
-  }
+    }),
+  };
 
-  let searchData = await fetch('/media/search_index.json').then(resp => resp.json())
+  const searchData =
+    await fetch('/media/search_index.json')
+      .then(resp => resp.json());
 
-  Object.entries(searchData).forEach(key_index_pair => {
-    const [index_key, index_data] = key_index_pair
-    Object.entries(index_data).forEach(key_value_pair => {
-      const [key, value] = key_value_pair
+  for (const [indexName, indexData] of Object.entries(searchData)) {
+    for (const [key, value] of Object.entries(indexData)) {
       window.indexes[index_key].import(key, value);
-    })
-  })
+    }
+  }
 }
 
-function searchAll(query, options) {
-  options = options || {}
-  return Object.entries(window.indexes).reduce((a, pair) => {
-    const [k, v] = pair
-    a[k] = v.search(query, options)
-    return a
-  }, {})
+function searchAll(query, options = {}) {
+  const results = {};
+
+  for (const [indexName, index] of Object.entries(window.indexes)) {
+    results[indexName] = index.search(query, options);
+  }
+
+  return results;
 }
 
 document.addEventListener('DOMContentLoaded', initSearch);
diff --git a/src/upd8.js b/src/upd8.js
index 358bf47b..17fd7d7c 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -47,12 +47,11 @@ import {bindFind, getAllFindSpecs} from '#find';
 import {processLanguageFile, watchLanguageFile, internalDefaultStringsFile}
   from '#language';
 import {isMain, traverse} from '#node-utils';
+import {writeSearchIndex} from '#search'
 import {sortByName} from '#sort';
 import {generateURLs, urlSpec} from '#urls';
 import {identifyAllWebRoutes} from '#web-routes';
 
-import {writeSearchIndex} from '#search'
-
 import {
   colors,
   decorateTime,
@@ -1481,10 +1480,10 @@ async function main() {
       timeStart: Date.now(),
     });
 
-    const search_index_path = path.join(mediaPath, "search_index.json")
-    logInfo(`Search index: ${search_index_path}`)
+    const searchIndexPath = path.join(mediaPath, "search_index.json");
+    logInfo`Search index: ${searchIndexPath}`;
 
-    await writeSearchIndex(search_index_path, wikiData)
+    await writeSearchIndex(searchIndexPath, wikiData);
 
     Object.assign(stepStatusSummary.buildSearchIndex, {
       status: STATUS_DONE_CLEAN,