diff --git a/src/js/util.js b/src/js/util.js index a059cec..53b5f46 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -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 diff --git a/test/util.test.js b/test/util.test.js index 4ff8bdd..6f429b3 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -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' +