feat: added option to search closest ekey

This commit is contained in:
鲁树人
2023-06-16 02:28:38 +01:00
parent 3433d2e860
commit 77c201c551
7 changed files with 169 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import type { DecryptCommandOptions } from '~/decrypt-worker/types';
import type { RootState } from '~/store';
import { closestByLevenshtein } from '~/util/levenshtein';
import { hasOwn } from '~/util/objects';
export const selectStagingQMCv2Settings = (state: RootState) => state.settings.staging.qmc2;
@@ -7,9 +8,21 @@ export const selectFinalQMCv2Settings = (state: RootState) => state.settings.pro
export const selectDecryptOptionByFile = (state: RootState, name: string): DecryptCommandOptions => {
const normalizedName = name.normalize();
const qmc2Keys = selectFinalQMCv2Settings(state).keys;
let qmc2Key: string | undefined;
const { keys: qmc2Keys, allowFuzzyNameSearch } = selectFinalQMCv2Settings(state);
if (hasOwn(qmc2Keys, normalizedName)) {
qmc2Key = qmc2Keys[normalizedName];
} else if (allowFuzzyNameSearch) {
const qmc2KeyStoreNames = Object.keys(qmc2Keys);
if (qmc2KeyStoreNames.length > 0) {
const closestName = closestByLevenshtein(normalizedName, qmc2KeyStoreNames);
console.debug('qmc2: key db could not find %o, using closest %o instead.', normalizedName, closestName);
qmc2Key = qmc2Keys[closestName];
}
}
return {
qmc2Key: hasOwn(qmc2Keys, normalizedName) ? qmc2Keys[normalizedName] : undefined,
qmc2Key,
};
};