Skip to content

Commit

Permalink
feat(index): round fix + improved quote fixer
Browse files Browse the repository at this point in the history
`json-fixer` can now fix several issues instead of one until it went through the heuristical-based
number of fixes needed for particular data. The quote fixer now doesn't includ commas or any
meaningful JSON tokens within the quotes.
  • Loading branch information
Berkmann18 committed May 19, 2019
1 parent 23d324f commit 6ecb55e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const {parse} = require('./src/json.pjs')
} */

const psw = d => process.stdout.write(`${d}\n`)
let fixRounds = 0
let roundThreshold = 20

const setFixThreshold = data => {
const lineCount = data.split('\n').length
roundThreshold = Math.max(data.length / lineCount, lineCount)
}

const doubleCheck = (data, verbose = false) => {
/* eslint-disable no-console */
Expand All @@ -22,6 +29,8 @@ const doubleCheck = (data, verbose = false) => {
psw('Nearly fixed data:')
data.split('\n').forEach((l, i) => psw(`${chalk.yellow(i)} ${l}`))
}
// eslint-disable-next-line no-use-before-define
if (fixRounds < roundThreshold) return fixJson(err, data, verbose)
console.error(chalk.red(`There's still an error!`))
throw new Error(err.message)
}
Expand Down Expand Up @@ -95,7 +104,7 @@ const fixMissingQuotes = ({start, fixedData, verbose}) => {
if (verbose) psw(chalk.magenta('Missing quotes'))
const targetLine = start.line - 1
const brokenLine = removeLinebreak(fixedData[targetLine])
const NO_RH_QUOTES = /(":\s*)([\s\S]+)/g
const NO_RH_QUOTES = /(":\s*)([^,{}[\]]+)/g
const NO_LH_QUOTES = /(^[^"]\S[\S\s]+)(:\s*["\w{[])/g
let fixedLine = NO_RH_QUOTES.test(brokenLine)
? brokenLine.replace(NO_RH_QUOTES, '$1"$2"')
Expand Down Expand Up @@ -170,6 +179,7 @@ const fixComment = ({start, fixedData, verbose}) => {

/*eslint-disable no-console */
const fixJson = (err, data, verbose) => {
++fixRounds
const lines = data.split('\n')
if (verbose) {
psw(`Data:`)
Expand Down Expand Up @@ -231,6 +241,8 @@ const checkJson = (data, verbose = false) => {
}
}
} catch (err) {
fixRounds = 0
setFixThreshold(data)
return {
data: fixJson(err, data, verbose),
changed: true,
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,30 @@ describe('comments', () => {
})
})
})

//ops and concats

describe('multi rounds', () => {
it('x2', () => {
const json = fs.readFileSync('./test/samples/twoErrs.json', 'utf-8')
const {data, changed} = jf(json)
expect(changed).toBeTruthy()
expect(data).toEqual({
name: 'sample #19',
type: 'JSON',
error: '2 errors',
version: 19,
})
})
it('x3', () => {
const json = fs.readFileSync('./test/samples/threeErrs.json', 'utf-8')
const {data, changed} = jf(json)
expect(changed).toBeTruthy()
expect(data).toEqual({
name: 'sample #21',
type: 'JSON',
error: '3 errors',
version: 21,
})
})
})
6 changes: 6 additions & 0 deletions test/samples/ops.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sample #20",
"type": "JSON",
"error": "comment",
"version": 2 * 10
}
6 changes: 6 additions & 0 deletions test/samples/threeErrs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sample #21",
"type": JSON,
"error": "3 errors"
"version": x15
}
6 changes: 6 additions & 0 deletions test/samples/twoErrs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sample #19",
"type": 'JSON',
"error": "2 errors",
"version": 19 //16
}

0 comments on commit 6ecb55e

Please sign in to comment.