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
4 changes: 2 additions & 2 deletions frontend/src/ts/elements/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as JSONData from "../utils/json-data";
import * as Hangul from "hangul-js";
import { showErrorNotification } from "../states/notifications";
import { getActivePage } from "../states/core";
import * as TestWords from "../test/test-words";
import { onCapsLockChange, isCapsLockOn } from "@leonabcd123/modern-caps-lock";
import * as ShiftTracker from "../test/shift-tracker";
import * as AltTracker from "../test/alt-tracker";
Expand All @@ -20,6 +19,7 @@ import { requestDebouncedAnimationFrame } from "../utils/debounced-animation-fra
import { getTheme } from "../states/theme";

import { createEffectOn } from "../hooks/effects";
import { wordsHaveNumbers } from "../test/test-state";

export const keyDataDelimiter = "\uE000";
const keymap = qsr("#keymap");
Expand Down Expand Up @@ -431,7 +431,7 @@ export async function refresh(): Promise<void> {
}

const showTopRow =
(TestWords.hasNumbers && Config.keymapMode === "next") ||
(wordsHaveNumbers && Config.keymapMode === "next") ||
Config.keymapShowTopRow === "always" ||
(layoutData.keymapShowTopRow && Config.keymapShowTopRow !== "never");

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/ts/input/helpers/word-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export async function goToNextWord({

PaceCaret.handleSpace(correctInsert, TestWords.words.getCurrentText());

Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex + 1));
const nextWord = TestWords.words.getText(TestState.activeWordIndex + 1);
if (nextWord !== undefined) Funbox.toggleScript(nextWord);

const lastWord = TestState.activeWordIndex >= TestWords.words.length - 1;
if (lastWord) {
Expand Down Expand Up @@ -97,7 +98,8 @@ export function goToPreviousWord(

TestState.decreaseActiveWordIndex();

Funbox.toggleScript(TestWords.words.getText(TestState.activeWordIndex));
const word = TestWords.words.getText(TestState.activeWordIndex);
if (word !== undefined) Funbox.toggleScript(word);

const nospaceEnabled = isFunboxActiveWithProperty("nospace");

Expand Down
8 changes: 6 additions & 2 deletions frontend/src/ts/test/british-english.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { capitalizeFirstLetterOfEachWord } from "../utils/strings";

export async function replace(
word: string,
previousWord: string,
previousWord: string | undefined,
): Promise<string> {
// Convert American-style double quotes to British-style single quotes
if (word.includes('"')) {
Expand Down Expand Up @@ -38,7 +38,11 @@ export async function replace(
? [rule, []]
: [rule.britishWord, rule.exceptPreviousWords];

if (Config.mode === "quote" && exceptions.includes(previousWord)) {
if (
Config.mode === "quote" &&
previousWord !== undefined &&
exceptions.includes(previousWord)
) {
return word;
}

Expand Down
10 changes: 7 additions & 3 deletions frontend/src/ts/test/pace-caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ function incrementLetterIndex(): void {
settings.currentLetterIndex++;
if (
settings.currentLetterIndex >=
TestWords.words.getText(settings.currentWordIndex).length + 1
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
TestWords.words.getText(settings.currentWordIndex)!.length + 1
) {
//go to the next word
settings.currentLetterIndex = 0;
Expand All @@ -193,7 +194,9 @@ function incrementLetterIndex(): void {
if (settings.currentLetterIndex <= -2) {
//go to the previous word
settings.currentLetterIndex =
TestWords.words.getText(settings.currentWordIndex - 1).length - 1;
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
TestWords.words.getText(settings.currentWordIndex - 1)!.length -
1;
settings.currentWordIndex--;
}
settings.correction++;
Expand All @@ -203,7 +206,8 @@ function incrementLetterIndex(): void {
settings.currentLetterIndex++;
if (
settings.currentLetterIndex >=
TestWords.words.getText(settings.currentWordIndex).length
// oxlint-disable-next-line typescript/no-non-null-assertion let it throw if undefined
TestWords.words.getText(settings.currentWordIndex)!.length
) {
//go to the next word
settings.currentLetterIndex = 0;
Expand Down
17 changes: 8 additions & 9 deletions frontend/src/ts/test/practise-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,16 @@ export function init(
if (missed === "biwords") {
for (let i = 0; i < TestWords.words.length; i++) {
const missedWord = TestWords.words.getText(i);

if (missedWord === undefined) continue; // won't happen, but ts complains

const missedWordCount = missedWords[missedWord];
if (missedWordCount !== undefined) {
if (i === 0) {
sortableMissedBiwords.push([missedWord, "", missedWordCount]);
} else {
sortableMissedBiwords.push([
missedWord,
TestWords.words.getText(i - 1),
missedWordCount,
]);
}
sortableMissedBiwords.push([
missedWord,
TestWords.words.getText(i - 1) ?? "",
missedWordCount,
]);
}
}
sortableMissedBiwords.sort((a, b) => {
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ async function init(): Promise<boolean> {
}
}

TestWords.setHasNumbers(hasNumbers);
TestState.setWordsHaveNumbers(hasNumbers);
setWordsHaveTab(wordsHaveTab);
setWordsHaveNewline(wordsHaveNewline);

Expand Down Expand Up @@ -1079,7 +1079,9 @@ export async function finish(difficultyFailed = false): Promise<void> {

const lastWordInputLength = history[wordIndex]?.length ?? 0;

if (lastWordInputLength < TestWords.words.getText(wordIndex).length) {
if (
lastWordInputLength < (TestWords.words.getText(wordIndex)?.length ?? 0)
) {
historyLength--;
}

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/ts/test/test-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export let resultVisible = false;
export let resultCalculating = false;
export let koreanStatus = false;
export let lastEventLog: EventLog | null = null;
export let wordsHaveNumbers = false;

export function setWordsHaveNumbers(val: boolean): void {
wordsHaveNumbers = val;
}

export function setLastEventLog(log: EventLog): void {
lastEventLog = log;
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/ts/test/test-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,9 @@ function showWords(): void {
} else {
let wordsHTML = "";
for (let i = 0; i < TestWords.words.length; i++) {
wordsHTML += buildWordHTML(TestWords.words.getText(i), i);
const word = TestWords.words.getText(i);
if (word === undefined) continue; // won't happen, but ts complains
wordsHTML += buildWordHTML(word, i);
}
wordsEl.setHtml(wordsHTML);
}
Expand Down Expand Up @@ -737,7 +739,7 @@ export async function updateWordLetters({
async () => {
pendingWordData.delete(wordIndex);
const currentWord = TestWords.words.getText(wordIndex);
if (!currentWord && Config.mode !== "zen") return;
if (currentWord === undefined && Config.mode !== "zen") return;
let ret = "";
const wordAtIndex = getWordElement(wordIndex);
if (!wordAtIndex) return;
Expand Down Expand Up @@ -767,7 +769,7 @@ export async function updateWordLetters({
const funbox = findSingleActiveFunboxWithFunction("getWordHtml");

const inputChars = Strings.splitIntoCharacters(input);
const currentWordChars = Strings.splitIntoCharacters(currentWord);
const currentWordChars = Strings.splitIntoCharacters(currentWord ?? "");
for (let i = 0; i < inputChars.length; i++) {
const charCorrect = currentWordChars[i] === inputChars[i];

Expand Down Expand Up @@ -867,7 +869,7 @@ export async function updateWordLetters({
hintsHtml = createHintsHtml(
hintIndices,
wordAtIndexLetters,
currentWord,
currentWord ?? "",
);
} else {
hintsHtml = createHintsHtml(hintIndices, wordAtIndexLetters, input);
Expand Down
7 changes: 1 addition & 6 deletions frontend/src/ts/test/test-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Words {
}

getText(i?: undefined, raw?: boolean): string[];
getText(i: number, raw?: boolean): string;
getText(i: number, raw?: boolean): string | undefined;
getText(i?: number, raw = false): string | string[] | undefined {
if (i === undefined) {
return this.list;
Expand Down Expand Up @@ -56,8 +56,3 @@ class Words {
}

export const words = new Words();
export let hasNumbers = false;

export function setHasNumbers(tf: boolean): void {
hasNumbers = tf;
}
15 changes: 9 additions & 6 deletions frontend/src/ts/test/words-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function shouldCapitalize(lastChar: string): boolean {

let spanishSentenceTracker = "";
export async function punctuateWord(
previousWord: string,
previousWord: string | undefined,
currentWord: string,
index: number,
maxindex: number,
Expand All @@ -47,7 +47,8 @@ export async function punctuateWord(

const currentLanguage = Config.language.split("_")[0];

const lastChar = Strings.getLastChar(previousWord);
const lastChar =
previousWord !== undefined ? Strings.getLastChar(previousWord) : undefined;

const funbox = findSingleActiveFunboxWithFunction("punctuateWord");
if (funbox) {
Expand All @@ -56,7 +57,7 @@ export async function punctuateWord(
if (
currentLanguage !== "code" &&
currentLanguage !== "georgian" &&
(index === 0 || shouldCapitalize(lastChar))
(index === 0 || (lastChar !== undefined && shouldCapitalize(lastChar)))
) {
//always capitalise the first word or if there was a dot unless using a code alphabet or the Georgian language

Expand Down Expand Up @@ -371,7 +372,7 @@ function applyFunboxesToWord(

async function applyBritishEnglishToWord(
word: string,
previousWord: string,
previousWord: string | undefined,
): Promise<string> {
if (!Config.britishEnglish) return word;
if (!Config.language.includes("english")) return word;
Expand Down Expand Up @@ -743,7 +744,7 @@ type GetNextWordReturn = {
export async function getNextWord(
wordIndex: number,
wordsBound: number,
previousWord: string,
previousWord: string | undefined,
previousWord2: string | undefined,
): Promise<GetNextWordReturn> {
console.debug("Getting next word", {
Expand Down Expand Up @@ -807,7 +808,9 @@ export async function getNextWord(

const funboxFrequency = getFunboxWordsFrequency() ?? "normal";
let randomWord = currentWordset.randomWord(funboxFrequency);
const previousWordRaw = previousWord.replace(/[.?!":\-,]/g, "").toLowerCase();
const previousWordRaw = previousWord
?.replace(/[.?!":\-,]/g, "")
.toLowerCase();
const previousWord2Raw = previousWord2
?.replace(/[.?!":\-,']/g, "")
.toLowerCase();
Expand Down
Loading