Skip to content

Commit

Permalink
fix: improve the logic to determine whether a JSON document needs for…
Browse files Browse the repository at this point in the history
…matting
  • Loading branch information
josdejong committed Feb 15, 2024
1 parent 4588c13 commit bb15bd1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/lib/utils/jsonUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ describe('jsonUtils', () => {
expect(needsFormatting('{\n "a": "some:message"\n}')).toBe(false)
expect(needsFormatting('{\n "a": "some,message"\n}')).toBe(false)

expect(needsFormatting('\n[1,2,3]')).toBe(true)
expect(needsFormatting('[1,2,3]\n')).toBe(true)
expect(needsFormatting('[1,2,3]\n ')).toBe(true)
expect(needsFormatting(' \n[1,2,3]')).toBe(true)

// a colon or comma inside a string gives a false positive (when the text doesn't contain a return character)
expect(needsFormatting('{"a": "some:message"}')).toBe(true)
expect(needsFormatting('{"a": "some,message"}')).toBe(true)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/jsonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export function isEqualParser(a: JSONParser, b: JSONParser): boolean {
*/
export function needsFormatting(jsonText: string): boolean {
const maxLength = 999
const head = jsonText.substring(0, maxLength)
const head = jsonText.substring(0, maxLength).trim()
return !head.includes('\n') && DELIMITER_WITHOUT_SPACING_REGEX.test(head)
}

Expand Down

0 comments on commit bb15bd1

Please sign in to comment.