From dda81c515ad85993aae7ab10a1a66e83a713b678 Mon Sep 17 00:00:00 2001 From: evgenius1424 Date: Wed, 5 Jun 2024 22:06:17 +0200 Subject: [PATCH] Refactor get-words.ts to streamline data parsing The changes in get-words.ts attempt to make the data parsing process more efficient by consolidating two separate actions into one. We specifically eliminated the separate 'word' parsing operation and directly supplied the parsed 'jsonObject' to 'zodParse'. Moreover, this refactor ensures all yielded words are in lower case. --- apps/learnbefore-bff/src/get-words.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/learnbefore-bff/src/get-words.ts b/apps/learnbefore-bff/src/get-words.ts index 6e574ee..5321cf6 100644 --- a/apps/learnbefore-bff/src/get-words.ts +++ b/apps/learnbefore-bff/src/get-words.ts @@ -28,14 +28,13 @@ export async function* getWords( const jsonObject = data.slice(startIndex, endIndex + 1) data = data.slice(endIndex + 1) try { - const word = parse(jsonObject) - const zodParse = wordSchema.safeParse(word) + const zodParse = wordSchema.safeParse(parse(jsonObject)) if (zodParse.success) { - yield word + yield { ...zodParse.data, word: zodParse.data.word.toLowerCase() } } else { console.error(`Zod validation error.`) console.error("Word validation error. " + zodParse.error.message) - console.error(`Parsed content: `, word) + console.error(`Parsed content: `, parse(jsonObject)) } } catch (err) { console.error("Error while parsing JSON:", err)