Create _containsMarker to reduce redundant code

This commit is contained in:
toasted-nutbread 2020-04-01 21:12:59 -04:00
parent a49e061545
commit 97b7b521dd

View File

@ -85,17 +85,7 @@ class AnkiNoteBuilder {
} }
async injectAudio(definition, fields, sources, audioSystem, optionsContext) { async injectAudio(definition, fields, sources, audioSystem, optionsContext) {
let usesAudio = false; if (!this._containsMarker(fields, 'audio')) { return; }
for (const fieldValue of Object.values(fields)) {
if (fieldValue.includes('{audio}')) {
usesAudio = true;
break;
}
}
if (!usesAudio) {
return true;
}
try { try {
const expressions = definition.expressions; const expressions = definition.expressions;
@ -114,17 +104,7 @@ class AnkiNoteBuilder {
} }
async injectScreenshot(definition, fields, screenshot, anki) { async injectScreenshot(definition, fields, screenshot, anki) {
let usesScreenshot = false; if (!this._containsMarker(fields, 'screenshot')) { return; }
for (const fieldValue of Object.values(fields)) {
if (fieldValue.includes('{screenshot}')) {
usesScreenshot = true;
break;
}
}
if (!usesScreenshot) {
return;
}
const now = new Date(Date.now()); const now = new Date(Date.now());
const filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`; const filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`;
@ -160,6 +140,16 @@ class AnkiNoteBuilder {
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`; return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
} }
_containsMarker(fields, marker) {
marker = `{${marker}}`;
for (const fieldValue of Object.values(fields)) {
if (fieldValue.includes(marker)) {
return true;
}
}
return false;
}
static stringReplaceAsync(str, regex, replacer) { static stringReplaceAsync(str, regex, replacer) {
let match; let match;
let index = 0; let index = 0;