Skip to content

Commit

Permalink
Refactor get-words.ts to streamline data parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evgenius1424 committed Jun 5, 2024
1 parent 8089078 commit dda81c5
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions apps/learnbefore-bff/src/get-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dda81c5

Please sign in to comment.