Skip to content

Commit

Permalink
Add guard clauses against dictionaryInfo.count being nullable (#1651)
Browse files Browse the repository at this point in the history
* Add guard clause

* Add another guard clause

* Add other guard clauses
  • Loading branch information
jamesmaa authored Dec 10, 2024
1 parent e38c1f0 commit 0acaad3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ext/js/dictionary/dictionary-data-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function groupTermFrequencies(dictionaryEntry, dictionaryInfo) {
});
}
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary);
const freqCount = currentDictionaryInfo?.counts.termMeta.freq ?? 0;
const freqCount = currentDictionaryInfo?.counts?.termMeta.freq ?? 0;
results.push({dictionary, frequencies, dictionaryAlias, freqCount});
}
return results;
Expand Down Expand Up @@ -138,7 +138,7 @@ export function groupKanjiFrequencies(sourceFrequencies, dictionaryInfo) {
});
}
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary);
const freqCount = currentDictionaryInfo?.counts.kanjiMeta.freq ?? 0;
const freqCount = currentDictionaryInfo?.counts?.kanjiMeta.freq ?? 0;
results.push({dictionary, frequencies, dictionaryAlias, freqCount});
}
return results;
Expand Down
8 changes: 4 additions & 4 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ export class DisplayGenerator {
dictionaryContentArray.push('URL: ' + currentDictionaryInfo.url);
}

const totalTerms = currentDictionaryInfo.counts.terms.total;
if (totalTerms > 0) {
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
if (!!totalTerms && totalTerms > 0) {
dictionaryContentArray.push('Term Count: ' + totalTerms.toString());
}

Expand Down Expand Up @@ -218,8 +218,8 @@ export class DisplayGenerator {
dictionaryContentArray.push('URL: ' + currentDictionaryInfo.url);
}

const totalKanji = currentDictionaryInfo.counts.kanji.total;
if (totalKanji > 0) {
const totalKanji = currentDictionaryInfo?.counts?.kanji?.total;
if (!!totalKanji && totalKanji > 0) {
dictionaryContentArray.push('Kanji Count: ' + totalKanji.toString());
}

Expand Down
4 changes: 2 additions & 2 deletions ext/js/pages/settings/dictionary-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ class DictionaryEntry {
outdateElement.hidden = (version >= 3);
countsElement.textContent = this._counts !== null ? JSON.stringify(this._counts, null, 4) : '';
wildcardSupportedElement.checked = prefixWildcardsSupported;
partsOfSpeechFilterSetting.hidden = !counts.terms.total;
partsOfSpeechFilterSetting.hidden = !counts?.terms.total;
partsOfSpeechFilterToggle.dataset.setting = `dictionaries[${this._index}].partsOfSpeechFilter`;

useDeinflectionsSetting.hidden = !counts.terms.total;
useDeinflectionsSetting.hidden = !counts?.terms.total;
useDeinflectionsToggle.dataset.setting = `dictionaries[${this._index}].useDeinflections`;

this._setupDetails(detailsTableElement);
Expand Down
2 changes: 1 addition & 1 deletion types/ext/dictionary-importer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type Summary = {
version: number;
importDate: number;
prefixWildcardsSupported: boolean;
counts: SummaryCounts;
counts?: SummaryCounts;
styles: string;
isUpdatable?: boolean;
indexUrl?: string;
Expand Down

0 comments on commit 0acaad3

Please sign in to comment.