Skip to content

Commit

Permalink
fix(fixer): improved trailing character fixer
Browse files Browse the repository at this point in the history
It can now fix trailing character that are at the end of a line right before an empty one

fix #18
  • Loading branch information
Berkmann18 committed Sep 18, 2019
1 parent fbf17a4 commit d671f0c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/samples/trailingChar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"test1": "1",
"test2": {
"a": "b",
"c": {

},

}
}
]

0 comments on commit d671f0c

Please sign in to comment.