From d1b7f655df1918f04e013acbb4ec0eb36122c40b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 26 Mar 2024 15:07:56 -0300 Subject: search: code style cleanup (no logic changes) --- src/static/clientSearch.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/static') 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); -- cgit 1.3.0-6-gf8a5