From 0011afe435f565dc5669f030da64e2c6e10ce05e Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:43:27 +0700 Subject: improve dictionary validation (#520) * improve dictionary validation * fix * improve performance * add undefined check * rename types * rename types * rename --- ext/js/dictionary/dictionary-importer.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'ext/js/dictionary') diff --git a/ext/js/dictionary/dictionary-importer.js b/ext/js/dictionary/dictionary-importer.js index a9726aaf..067c8a3a 100644 --- a/ext/js/dictionary/dictionary-importer.js +++ b/ext/js/dictionary/dictionary-importer.js @@ -115,13 +115,13 @@ export class DictionaryImporter { // Files /** @type {import('dictionary-importer').QueryDetails} */ - const queryDetails = new Map([ + const queryDetails = [ ['termFiles', /^term_bank_(\d+)\.json$/], ['termMetaFiles', /^term_meta_bank_(\d+)\.json$/], ['kanjiFiles', /^kanji_bank_(\d+)\.json$/], ['kanjiMetaFiles', /^kanji_meta_bank_(\d+)\.json$/], ['tagFiles', /^tag_bank_(\d+)\.json$/] - ]); + ]; const {termFiles, termMetaFiles, kanjiFiles, kanjiMetaFiles, tagFiles} = Object.fromEntries(this._getArchiveFiles(fileMap, queryDetails)); // Load data @@ -692,16 +692,18 @@ export class DictionaryImporter { _getArchiveFiles(fileMap, queryDetails) { /** @type {import('dictionary-importer').QueryResult} */ const results = new Map(); - for (const [name, value] of fileMap.entries()) { - for (const [fileType, fileNameFormat] of queryDetails.entries()) { - let entries = results.get(fileType); - if (typeof entries === 'undefined') { - entries = []; - results.set(fileType, entries); - } - if (fileNameFormat.test(name)) { - entries.push(value); + for (const [fileType] of queryDetails) { + results.set(fileType, []); + } + + for (const [fileName, fileEntry] of fileMap.entries()) { + for (const [fileType, fileNameFormat] of queryDetails) { + if (!fileNameFormat.test(fileName)) { continue; } + const entries = results.get(fileType); + + if (typeof entries !== 'undefined') { + entries.push(fileEntry); break; } } -- cgit v1.2.3