Skip to content

Commit 6826610

Browse files
committed
refactor: move data normalization to a function
1 parent 43e0bc7 commit 6826610

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

frontend/src/ts/input/handlers/insert-text.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,17 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
9595
return;
9696
}
9797

98+
// input and target word
99+
const testInput = TestInput.input.current;
100+
const currentWord = TestWords.words.getCurrent();
101+
98102
// if the character is visually equal, replace it with the target character
99103
// this ensures all future equivalence checks work correctly
100-
let normalizedData: string | null = null;
101-
const targetChar =
102-
TestWords.words.getCurrent()[TestInput.input.current.length];
103-
if (
104-
targetChar !== undefined &&
105-
areCharactersVisuallyEqual(options.data, targetChar, Config.language)
106-
) {
107-
replaceInputElementLastValueChar(targetChar);
108-
normalizedData = targetChar;
109-
}
110-
104+
const normalizedData = normalizeDataAndUpdateInputIfNeeded(
105+
options.data,
106+
testInput,
107+
currentWord
108+
);
111109
const data = normalizedData ?? options.data;
112110

113111
// start if needed
@@ -118,8 +116,6 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
118116
// helper consts
119117
const lastInMultiOrSingle =
120118
lastInMultiIndex === true || lastInMultiIndex === undefined;
121-
const testInput = TestInput.input.current;
122-
const currentWord = TestWords.words.getCurrent();
123119
const wordIndex = TestState.activeWordIndex;
124120
const charIsSpace = isSpace(data);
125121
const charIsNewline = data === "\n";
@@ -289,6 +285,23 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
289285
}
290286
}
291287

288+
function normalizeDataAndUpdateInputIfNeeded(
289+
data: string,
290+
testInput: string,
291+
currentWord: string
292+
): string | null {
293+
let normalizedData: string | null = null;
294+
const targetChar = currentWord[testInput.length];
295+
if (
296+
targetChar !== undefined &&
297+
areCharactersVisuallyEqual(data, targetChar, Config.language)
298+
) {
299+
replaceInputElementLastValueChar(targetChar);
300+
normalizedData = targetChar;
301+
}
302+
return normalizedData;
303+
}
304+
292305
export async function emulateInsertText(
293306
options: OnInsertTextParams
294307
): Promise<void> {

0 commit comments

Comments
 (0)