Skip to content

Commit

Permalink
feat: #125 strip leading commas in objects and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed May 15, 2024
1 parent 019e509 commit 0ee8597
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,20 @@ describe.each(implementations)('jsonrepair [$name]', ({ jsonrepair }) => {
expect(jsonrepair('\\"hello"')).toBe('"hello"')
})

test('should strip a leading comma from an array', () => {
expect(jsonrepair('[,1,2,3]')).toBe('[1,2,3]')
expect(jsonrepair('[/* a */,/* b */1,2,3]')).toBe('[1,2,3]')
expect(jsonrepair('[, 1,2,3]')).toBe('[ 1,2,3]')
expect(jsonrepair('[ , 1,2,3]')).toBe('[ 1,2,3]')
})

test('should strip a leading comma from an object', () => {
expect(jsonrepair('{,"message": "hi"}')).toBe('{"message": "hi"}')
expect(jsonrepair('{/* a */,/* b */"message": "hi"}')).toBe('{"message": "hi"}')
expect(jsonrepair('{ ,"message": "hi"}')).toBe('{ "message": "hi"}')
expect(jsonrepair('{, "message": "hi"}')).toBe('{ "message": "hi"}')
})

test('should strip trailing commas from an array', () => {
expect(jsonrepair('[1,2,3,]')).toBe('[1,2,3]')
expect(jsonrepair('[1,2,3,\n]')).toBe('[1,2,3\n]')
Expand Down
10 changes: 10 additions & 0 deletions src/regular/jsonrepair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export function jsonrepair(text: string): string {
i++
parseWhitespaceAndSkipComments()

// repair: skip leading comma like in {, message: "hi"}
if (skipCharacter(codeComma)) {
parseWhitespaceAndSkipComments()
}

let initial = true
while (i < text.length && text.charCodeAt(i) !== codeClosingBrace) {
let processedComma
Expand Down Expand Up @@ -327,6 +332,11 @@ export function jsonrepair(text: string): string {
i++
parseWhitespaceAndSkipComments()

// repair: skip leading comma like in [,1,2,3]
if (skipCharacter(codeComma)) {
parseWhitespaceAndSkipComments()
}

let initial = true
while (i < text.length && text.charCodeAt(i) !== codeClosingBracket) {
if (!initial) {
Expand Down
10 changes: 10 additions & 0 deletions src/streaming/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ export function jsonrepairCore({
function parseObjectStart(): boolean {
if (parseCharacter(codeOpeningBrace)) {
parseWhitespaceAndSkipComments()

if (skipCharacter(codeComma)) {
parseWhitespaceAndSkipComments()
}

if (parseCharacter(codeClosingBrace)) {
return stack.update(Caret.afterValue)
}
Expand All @@ -219,6 +224,11 @@ export function jsonrepairCore({
function parseArrayStart(): boolean {
if (parseCharacter(codeOpeningBracket)) {
parseWhitespaceAndSkipComments()

if (skipCharacter(codeComma)) {
parseWhitespaceAndSkipComments()
}

if (parseCharacter(codeClosingBracket)) {
return stack.update(Caret.afterValue)
}
Expand Down

0 comments on commit 0ee8597

Please sign in to comment.