Skip to content

Commit

Permalink
Fix newline starting new object with missing colon
Browse files Browse the repository at this point in the history
• detect if the current string we are inside should become a key and add missing closing quote mark and colon if so
• add a test to verify it
  • Loading branch information
acusti committed Mar 29, 2024
1 parent 92241c8 commit 7b8e8fc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/parsing/src/parse-as-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,57 @@ Props:
// first quote mark is recognized as an unescaped character, final quote mark is treated as string terminus
teamMemberJobTitle1: 'Product Here is the JSON output for the "Testimonials',
});

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
{
"heading": "Lava Lamps",
"subheading": "Choose from our wide selection of lava lamps in different sizes, colors, and features.",
"items": [
{
"heading": "Classic Lava Lamp",
"price": "$39.99",
"button": "Add to Cart",
"imageURL": "/classic-lava-lamp.jpg",
"imageAlt": "Classic Lava Lamp"
},
{
"heading": "Glowing Lava Lamp",
"price": "$44.99",
"button": "Add to Cart",
"imageURL": "/`;

expect(parseAsJSON(response)).toEqual({
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': {
heading: 'Lava Lamps',
subheading:
'Choose from our wide selection of lava lamps in different sizes, colors, and features.',
items: [
{
heading: 'Classic Lava Lamp',
price: '$39.99',
button: 'Add to Cart',
imageURL: '/classic-lava-lamp.jpg',
imageAlt: 'Classic Lava Lamp',
},
{
heading: 'Glowing Lava Lamp',
price: '$44.99',
button: 'Add to Cart',
imageURL: '/',
},
],
},
},
],
});
}

function preambleTestCase() {
Expand Down
3 changes: 3 additions & 0 deletions packages/parsing/src/parse-as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ export function parseAsJSON(text: string): ParsedValue | null {
newText.substring(keyStartIndex, lastColonIndex + 1) +
'"' +
newText.substring(lastColonIndex + 1);
} else if (getPreviousStringType(newText) === 'VALUE') {
// if previous string is a value, convert current string into a key
char = '":';
} else {
// if no key was found, add one
let keyIndex = 1;
Expand Down

0 comments on commit 7b8e8fc

Please sign in to comment.