Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions frontend/src/ts/test/events/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/ts/test/events/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/ts/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading