Skip to content

Commit

Permalink
Handle missing values in objects (+ new test case)
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Mar 29, 2024
1 parent 85139f8 commit f8f0ccc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/parsing/src/parse-as-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('@acusti/parsing', () => {
it('handles extraneous escape characters', extraneousEscapeCharactersTestCase);
it('handles unescaped double quotes', unescapedDoubleQuotesTestCase);
it('detects premature closing curly brace and ignores it', prematureClosingCurliesTestCase);
it('detects an object key with a missing value and fills it in', missingValuesTestCase);
});
});

Expand Down Expand Up @@ -638,3 +639,35 @@ function prematureClosingCurliesTestCase() {
],
});
}

function missingValuesTestCase() {
const response = `\
\`\`\`json
{"heading":"Digg FAQ","subheading":"Get the Answers You Need","items":[{"heading":"What is Digg?","subheading":"","description":"Digg is an American news aggregator that curates a front page of the most popular stories on the Internet. It covers topics such as science, trending political issues, and viral Internet issues.","button":""},{"heading":"How does Digg work?","subheading","description":"Digg uses a combination of editorial curation and algorithmic sorting to select the most popular stories on the Internet. Users can submit stories, which are then voted on by other users. The stories with the most votes appear on the Digg front page.","button":""},{"heading":"How do I submit a story to Digg?","subheading","description":"To submit a story to Digg, you need to create an account and then click on the 'Submit a Story' button. You will then be prompted to enter the URL of the story you want to submit. Once submitted, other users can vote on your story, and if it gains enough votes, it may appear on the Digg front page.","button":""}]}
\`\`\``;

expect(parseAsJSON(response)).toEqual({
heading: 'Digg FAQ',
subheading: 'Get the Answers You Need',
items: [
{
heading: 'What is Digg?',
subheading: "",
description: 'Digg is an American news aggregator that curates a front page of the most popular stories on the Internet. It covers topics such as science, trending political issues, and viral Internet issues.',
button: "",
},
{
heading: 'How does Digg work?',
subheading: '',
description: 'Digg uses a combination of editorial curation and algorithmic sorting to select the most popular stories on the Internet. Users can submit stories, which are then voted on by other users. The stories with the most votes appear on the Digg front page.',
button: "",
},
{
heading: 'How do I submit a story to Digg?',
subheading: '',
description: 'To submit a story to Digg, you need to create an account and then click on the \'Submit a Story\' button. You will then be prompted to enter the URL of the story you want to submit. Once submitted, other users can vote on your story, and if it gains enough votes, it may appear on the Digg front page.',
button: "",
}
],
});
}
12 changes: 12 additions & 0 deletions packages/parsing/src/parse-as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ export function parseAsJSON(text: string): ParsedValue | null {
char = ',';
}
}
} else if (char === ',' && stack.at(-1) === '}') {
// ensure this follows a full key/value pair
const lastEndKeyIndexA = newText.lastIndexOf('":"');
const lastEndKeyIndexB = newText.lastIndexOf('": "');
const lastEndKeyIndex = Math.max(lastEndKeyIndexA, lastEndKeyIndexB);
const lastEndValueIndexA = newText.lastIndexOf('","');
const lastEndValueIndexB = newText.lastIndexOf('", "');
const lastEndValueIndex = Math.max(lastEndValueIndexA, lastEndValueIndexB);
// if the last string is a value (not a key), add an empty value
if (lastEndValueIndex > 0 && lastEndKeyIndex > 0 && lastEndValueIndex > lastEndKeyIndex) {
char = ': "",';
}
}
}

Expand Down

0 comments on commit f8f0ccc

Please sign in to comment.