aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCashew <52880648+Scrub1492@users.noreply.github.com>2023-12-21 23:33:49 +0900
committerGitHub <noreply@github.com>2023-12-21 14:33:49 +0000
commitb83ca2f37d6bb1007f62216cebf96a1177e556dc (patch)
tree47d37de584be77cc00b209e2af1d8b54e265f9f0
parentf041f808891d6fc16e02a05b96a398ab432c756a (diff)
Minor optimizations for dictionary import (#412)
* minor dictionary import optimizations * fix comment * use regex for matching
-rw-r--r--ext/js/language/dictionary-importer.js40
-rw-r--r--types/ext/dictionary-importer.d.ts10
2 files changed, 35 insertions, 15 deletions
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;
diff --git a/types/ext/dictionary-importer.d.ts b/types/ext/dictionary-importer.d.ts
index 5ae20dd1..cccfdc42 100644
--- a/types/ext/dictionary-importer.d.ts
+++ b/types/ext/dictionary-importer.d.ts
@@ -98,6 +98,16 @@ export type ImportRequirementContext = {
export type ArchiveFileMap = Map<string, ZipJS.Entry>;
+/**
+ * A map of file types inside a dictionary and its corresponding regular expressions.
+ */
+export type QueryDetails = Map<string, RegExp>;
+
+/**
+ * A map of file types inside a dictionary and its matching entries.
+ */
+export type QueryResult = Map<string, ZipJS.Entry[]>;
+
export type CompiledSchemaNameArray = [
termBank: CompiledSchemaName,
termMetaBank: CompiledSchemaName,