From d671f0c68d62bbb11919fdad3bf128f71fb20d84 Mon Sep 17 00:00:00 2001 From: Berkmann18 Date: Wed, 18 Sep 2019 12:45:39 +0100 Subject: [PATCH] fix(fixer): improved trailing character fixer It can now fix trailing character that are at the end of a line right before an empty one fix #18 --- src/__tests__/index.js | 15 +++++++++++++++ src/fixer.js | 2 ++ test/samples/trailingChar.json | 12 ++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 test/samples/trailingChar.json diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 0570ed1..79a5d78 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -112,6 +112,21 @@ describe('fix trailing characters', () => { }) }) + it('chars', () => { + const json = fs.readFileSync('./test/samples/trailingChar.json', 'utf-8') + const {data, changed} = jf(json) + expect(changed).toBeTruthy() + expect(data).toEqual([ + { + test1: '1', + test2: { + a: 'b', + c: {}, + }, + }, + ]) + }) + it('hex\'s "x"', () => { const json = fs.readFileSync('./test/samples/x.json', 'utf-8') const {data, changed} = jf(json) diff --git a/src/fixer.js b/src/fixer.js index e773f64..885a116 100644 --- a/src/fixer.js +++ b/src/fixer.js @@ -4,7 +4,9 @@ const {parse} = require('./json.pjs') const fixExtraChar = ({fixedData, verbose, targetLine}) => { if (verbose) psw(chalk.magenta('Extra character')) + if (fixedData[targetLine] === '') --targetLine const brokenLine = removeLinebreak(fixedData[targetLine]) + let fixedLine = brokenLine.trimEnd() fixedLine = fixedLine.substr(0, fixedLine.length - 1) fixedData[targetLine] = fixedLine diff --git a/test/samples/trailingChar.json b/test/samples/trailingChar.json new file mode 100644 index 0000000..6c9fdc1 --- /dev/null +++ b/test/samples/trailingChar.json @@ -0,0 +1,12 @@ +[ + { + "test1": "1", + "test2": { + "a": "b", + "c": { + + }, + + } + } +] \ No newline at end of file