Skip to content

Commit

Permalink
Fix partial response edge case handling + new test
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Apr 22, 2024
1 parent c1eb0e3 commit 5f68437
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/parsing/src/parse-as-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/parsing/src/parse-as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f68437

Please sign in to comment.