From b83ca2f37d6bb1007f62216cebf96a1177e556dc Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Thu, 21 Dec 2023 23:33:49 +0900 Subject: Minor optimizations for dictionary import (#412) * minor dictionary import optimizations * fix comment * use regex for matching --- ext/js/language/dictionary-importer.js | 40 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'ext/js') diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index fa16ec7a..2b9792ea 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -113,11 +113,15 @@ export class DictionaryImporter { const dataBankSchemas = this._getDataBankSchemas(version); // Files - const termFiles = this._getArchiveFiles(fileMap, 'term_bank_?.json'); - const termMetaFiles = this._getArchiveFiles(fileMap, 'term_meta_bank_?.json'); - const kanjiFiles = this._getArchiveFiles(fileMap, 'kanji_bank_?.json'); - const kanjiMetaFiles = this._getArchiveFiles(fileMap, 'kanji_meta_bank_?.json'); - const tagFiles = this._getArchiveFiles(fileMap, 'tag_bank_?.json'); + /** @type {import('dictionary-importer').QueryDetails} */ + const queryDetails = new Map([ + ['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 this._progressNextStep(termFiles.length + termMetaFiles.length + kanjiFiles.length + kanjiMetaFiles.length + tagFiles.length); @@ -679,18 +683,24 @@ export class DictionaryImporter { /** * @param {import('dictionary-importer').ArchiveFileMap} fileMap - * @param {string} fileNameFormat - * @returns {import('@zip.js/zip.js').Entry[]} + * @param {import('dictionary-importer').QueryDetails} queryDetails + * @returns {import('dictionary-importer').QueryResult} */ - _getArchiveFiles(fileMap, fileNameFormat) { - const indexPosition = fileNameFormat.indexOf('?'); - const prefix = fileNameFormat.substring(0, indexPosition); - const suffix = fileNameFormat.substring(indexPosition + 1); - /** @type {import('@zip.js/zip.js').Entry[]} */ - const results = []; + _getArchiveFiles(fileMap, queryDetails) { + /** @type {import('dictionary-importer').QueryResult} */ + const results = new Map(); for (const [name, value] of fileMap.entries()) { - if (name.startsWith(prefix) && name.endsWith(suffix)) { - results.push(value); + 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); + break; + } } } return results; -- cgit v1.2.3