Skip to content

Commit 9809553

Browse files
committed
refactor loadWordsHistory
1 parent dbfdb21 commit 9809553

1 file changed

Lines changed: 27 additions & 29 deletions

File tree

frontend/src/ts/test/test-ui.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,32 +1257,46 @@ export function setLigatures(isEnabled: boolean): void {
12571257
}
12581258

12591259
function buildWordLettersHTML(
1260-
charCount: number,
12611260
input: string,
12621261
corrected: string,
1263-
inputCharacters: string[],
1264-
wordCharacters: string[],
1265-
correctedCharacters: string[],
1262+
word: string,
12661263
containsKorean: boolean,
12671264
): string {
1265+
const inputCharacters = Strings.splitIntoCharacters(input);
1266+
const correctedCharacters = Strings.splitIntoCharacters(corrected);
1267+
const wordCharacters = Strings.splitIntoCharacters(word);
1268+
1269+
const koreanCorrectedCharacters = Strings.splitIntoCharacters(
1270+
Hangul.assemble(corrected.split("")),
1271+
);
1272+
1273+
let loopCharCount;
1274+
if (Config.mode === "zen" || inputCharacters.length > wordCharacters.length) {
1275+
//input is longer - extra characters possible (loop over input)
1276+
loopCharCount = inputCharacters.length;
1277+
} else {
1278+
//input is shorter or equal (loop over word list)
1279+
loopCharCount = wordCharacters.length;
1280+
}
1281+
12681282
let out = "";
1269-
for (let c = 0; c < charCount; c++) {
1283+
for (let c = 0; c < loopCharCount; c++) {
12701284
let correctedChar;
12711285
try {
12721286
correctedChar = !containsKorean
12731287
? correctedCharacters[c]
1274-
: Hangul.assemble(corrected.split(""))[c];
1288+
: koreanCorrectedCharacters[c];
12751289
} catch (e) {
12761290
correctedChar = undefined;
12771291
}
12781292
let extraCorrected = "";
1279-
const historyWord: string = !containsKorean
1280-
? corrected
1281-
: Hangul.assemble(corrected.split(""));
1293+
const historyChars = !containsKorean
1294+
? correctedCharacters
1295+
: koreanCorrectedCharacters;
12821296
if (
1283-
c + 1 === charCount &&
1284-
historyWord !== undefined &&
1285-
historyWord.length > input.length
1297+
c + 1 === loopCharCount &&
1298+
historyChars !== undefined &&
1299+
historyChars.length > input.length
12861300
) {
12871301
extraCorrected = "extraCorrected";
12881302
}
@@ -1378,28 +1392,12 @@ async function loadWordsHistory(): Promise<boolean> {
13781392
wordEl.setAttribute("input", input.replace(/ /g, "_"));
13791393
}
13801394

1381-
const inputCharacters = Strings.splitIntoCharacters(input);
1382-
const wordCharacters = Strings.splitIntoCharacters(word);
1383-
const correctedCharacters = Strings.splitIntoCharacters(corrected ?? "");
1384-
1385-
let loop;
1386-
if (Config.mode === "zen" || input.length > word.length) {
1387-
//input is longer - extra characters possible (loop over input)
1388-
loop = inputCharacters.length;
1389-
} else {
1390-
//input is shorter or equal (loop over word list)
1391-
loop = wordCharacters.length;
1392-
}
1393-
13941395
if (corrected === undefined) throw new Error("empty corrected word");
13951396

13961397
wordEl.innerHTML = buildWordLettersHTML(
1397-
loop,
13981398
input,
13991399
corrected,
1400-
inputCharacters,
1401-
wordCharacters,
1402-
correctedCharacters,
1400+
word,
14031401
containsKorean,
14041402
);
14051403
} catch (e) {

0 commit comments

Comments
 (0)