replaced .at with a function that is supported in older browsers

This commit is contained in:
Miodec 2023-05-05 19:30:46 +02:00
parent 198493ca95
commit 0e88bd87c3
3 changed files with 15 additions and 5 deletions

View file

@ -576,7 +576,9 @@ export async function init(): Promise<void> {
}
if (Config.keymapMode === "next" && Config.mode !== "zen") {
KeymapEvent.highlight(TestWords.words.getCurrent().at(0) as string);
KeymapEvent.highlight(
Misc.nthElementFromArray([...TestWords.words.getCurrent()], 0) as string
);
}
Funbox.toggleScript(TestWords.words.getCurrent());
TestUI.setRightToLeft(language.rightToLeft);

View file

@ -440,8 +440,8 @@ export async function generateWords(
i,
language,
limit,
ret.at(-1) ?? "",
ret.at(-2) ?? ""
Misc.nthElementFromArray(ret, -1) ?? "",
Misc.nthElementFromArray(ret, -2) ?? ""
);
ret.push(nextWord);
}
@ -528,8 +528,8 @@ async function generateQuoteWords(
i,
language,
limit,
ret.at(-1) ?? "",
ret.at(-2) ?? ""
Misc.nthElementFromArray(ret, -1) ?? "",
Misc.nthElementFromArray(ret, -2) ?? ""
);
ret.push(nextWord);
}

View file

@ -1282,6 +1282,14 @@ export function randomElementFromArray<T>(array: T[]): T {
return array[randomIntFromRange(0, array.length - 1)];
}
export function nthElementFromArray<T>(
array: T[],
index: number
): T | undefined {
index = index < 0 ? array.length + index : index;
return array[index];
}
export function randomElementFromObject<T extends object>(
object: T
): T[keyof T] {