From 5f684375b430aea8d777919e002200ab2dccc1c6 Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Mon, 22 Apr 2024 16:26:18 -0700 Subject: [PATCH] Fix partial response edge case handling + new test --- packages/parsing/src/parse-as-json.test.ts | 3 +++ packages/parsing/src/parse-as-json.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/parsing/src/parse-as-json.test.ts b/packages/parsing/src/parse-as-json.test.ts index 37cd4bd7..7105f8fb 100644 --- a/packages/parsing/src/parse-as-json.test.ts +++ b/packages/parsing/src/parse-as-json.test.ts @@ -357,6 +357,9 @@ Props: teamMemberJobTitle1: 'Product Here is the JSON output for the "Testimonials', }); + response = '```json\n{\n "heading": "News",\n "'; + expect(parseAsJSON(response)).toEqual({ heading: 'News', '': '' }); + response = `\ \`\`\`json {"items":[{"heading":"Welcome to Lava","subheading":"Your One-Stop Shop for Lava Lamps and Whisky Ice Cream","description":"Lava is a unique store located in the heart of Noe Valley, San Francisco. We specialize in selling a wide variety of lava lamps, from classic designs to modern and quirky creations. But that's not all! We also offer a delicious treat that's sure to tantalize your taste buds: free whisky ice cream for those who come in before noon. Come visit us and experience the Lava magic for yourself!","\`\`\`json diff --git a/packages/parsing/src/parse-as-json.ts b/packages/parsing/src/parse-as-json.ts index 553d1445..3c088d40 100644 --- a/packages/parsing/src/parse-as-json.ts +++ b/packages/parsing/src/parse-as-json.ts @@ -413,7 +413,7 @@ export function parseAsJSON(text: string): ParsedValue | null { if (stack.at(-1) === '}') { // if we are in the key of a key/value pair, append ': ""' to close the pair - if (/[{,][^:"]*"[^"]+"\s*?$/.test(newText)) { + if (/[{,][^:"]*"[^"]*"\s*?$/.test(newText)) { newText += ': ""'; } else if (/": ?$/.test(newText)) { // if we are in between a key/value pair, append '""' to close the pair