Skip to content

Commit

Permalink
fix: optimize slice
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Feb 4, 2024
1 parent cd20221 commit 0587b88
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions server/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ export default defineEventHandler(async (event) => {
const data = await getCouplets(query.prompt as string)
const { content } = data

const unwrapperContent = (content || '{}')?.replace('```json\n', '').replace('```', '')
let unWrapperContent = content || ''
const startPos = unWrapperContent.indexOf('{')
const endPos = unWrapperContent.lastIndexOf('}')

if (startPos === -1 || endPos === -1) {
// eslint-disable-next-line no-console
console.log(content)
return
}

unWrapperContent = unWrapperContent.slice(startPos, endPos + 1)
unWrapperContent = (unWrapperContent || '{}')?.replace('```json\n', '').replace('```', '')
let coupletData: SprintFestivalCouplets | undefined
try {
coupletData = JSON.parse(unwrapperContent) as SprintFestivalCouplets
coupletData = JSON.parse(unWrapperContent) as SprintFestivalCouplets
}
catch (e) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 0587b88

Please sign in to comment.