A fix in repairing line-separated json

This commit is contained in:
jos 2020-03-25 12:18:41 +01:00
parent fd0db5dc73
commit 4f433cb6a9
2 changed files with 18 additions and 2 deletions

View File

@ -109,6 +109,16 @@ export function repair (jsString) {
return jsString[iNext]
}
// get at the first non-white space character starting at the current character
function currNonWhiteSpace () {
let iNext = i
while (iNext < jsString.length && isWhiteSpace(jsString[iNext])) {
iNext++
}
return jsString[iNext]
}
// skip a block comment '/* ... */'
function skipBlockComment () {
if (curr() === '/' && next() === '*') {
@ -299,7 +309,7 @@ export function repair (jsString) {
const whitespaces = parseWhiteSpace()
skipBlockComment()
if (curr() === '{' || nextNonWhiteSpace() === '{') {
if (currNonWhiteSpace() === '{') {
chars.push(',')
if (indent === 0) {
isLineSeparatedJson = true

View File

@ -96,7 +96,7 @@ describe('util', () => {
assert.strictEqual(repair('callback({}'), 'callback({}')
})
it('should strip trailing zeros', () => {
it('should strip trailing commas', () => {
// matching
assert.strictEqual(repair('[1,2,3,]'), '[1,2,3]')
assert.strictEqual(repair('[1,2,3,\n]'), '[1,2,3\n]')
@ -161,6 +161,12 @@ describe('util', () => {
assert.strictEqual(repair(text), expected)
})
it('should not repair normal array with comma separated objects', () => {
const text = '[\n{},\n{}\n]'
assert.strictEqual(repair(text), text)
})
it('should repair line separated json (for example from robo3t', () => {
const text = '' +
'/* 1 */\n' +