diff --git a/frontend/src/ts/test/events/helpers.ts b/frontend/src/ts/test/events/helpers.ts index 0e4bf58860b7..c6228ddae1c2 100644 --- a/frontend/src/ts/test/events/helpers.ts +++ b/frontend/src/ts/test/events/helpers.ts @@ -121,25 +121,26 @@ export function applyInputEvent(input: string, event: InputEventNoMs): string { } export function getInputFromDom(events: TestEventNoMs[]): string { - const lastInputEvent = events.findLast((e) => e.type === "input"); - - if (lastInputEvent === undefined) { - let input = ""; - for (const event of events) { - if (event.type !== "input") continue; - input = applyInputEvent(input, event); + let lastInputEvent: InputEventNoMs | undefined; + for (let i = events.length - 1; i >= 0; i--) { + const e = events[i]; + if (e !== undefined && e.type === "input") { + lastInputEvent = e; + break; } - return input; } - const inputValue = lastInputEvent.data.inputValue; + if (lastInputEvent === undefined) return ""; + + const { data } = lastInputEvent; + const inputValue = data.inputValue; if ( - lastInputEvent.data.inputType === "insertText" && - lastInputEvent.data.data === " " && - lastInputEvent.data.lastWord && - lastInputEvent.data.commitsWord && - !lastInputEvent.data.correct + data.inputType === "insertText" && + data.data === " " && + data.lastWord && + data.commitsWord && + !data.correct ) { // if this is an incorrect word commit on the last word, we dont want to count it at all return inputValue.trimEnd(); diff --git a/frontend/src/ts/test/events/stats.ts b/frontend/src/ts/test/events/stats.ts index b0db9c30a18c..144737965cec 100644 --- a/frontend/src/ts/test/events/stats.ts +++ b/frontend/src/ts/test/events/stats.ts @@ -5,7 +5,7 @@ import { logTestEvent, } from "./data"; import * as TestWords from "../../test/test-words"; -import { CharCounts, countChars, getLastChar } from "../../utils/strings"; +import { CharCounts, countChars } from "../../utils/strings"; import * as CustomText from "../../test/custom-text"; import { getInputFromDom } from "./helpers"; import { activeWordIndex, bailedOut, koreanStatus } from "../test-state"; @@ -245,7 +245,11 @@ function getTargetWord( } else { const word = TestWords.words.getText(wordIndex); - if (getLastChar(word) === "\n") { + if (word === undefined) { + return ""; + } + + if (word.endsWith("\n")) { // for multiline, dont add space return word; } diff --git a/frontend/src/ts/utils/strings.ts b/frontend/src/ts/utils/strings.ts index 13e1dee0222e..0a455a363e6f 100644 --- a/frontend/src/ts/utils/strings.ts +++ b/frontend/src/ts/utils/strings.ts @@ -43,11 +43,8 @@ export function wordsToCamelCase(str: string): string { * @returns The last character of the input string, or an empty string if the input is empty. */ export function getLastChar(word: string): string { - try { - return word.charAt(word.length - 1); - } catch { - return ""; - } + if (word === undefined) return ""; + return word.charAt(word.length - 1); } /**