Skip to content

Commit

Permalink
fix json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdgustn committed Oct 16, 2021
1 parent ce2b4e8 commit 1317313
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
36 changes: 30 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"homepage": "https://github.com/wjdgustn/level-file-checker#readme",
"devDependencies": {
"pkg": "^5.1.0"
},
"dependencies": {
"json5": "^2.2.0"
}
}
22 changes: 14 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const JSON5 = require('json5');

String.prototype.replaceAll = function(org, dest) {
return this.split(org).join(dest);
}

module.exports.ADOFAIParser = level => {
return JSON.parse(String(level).trim()
.replaceAll(', ,', ',')
.replaceAll('}\n', '},\n')
.replaceAll('},\n\t]', '}\n\t]')
.replaceAll(', },', ' },')
.replaceAll(', }', ' }')
.replaceAll('\n', '')
.replaceAll('}\n', '},\n'));
try {
return JSON5.parse(String(level).trim());
} catch (e) {
return JSON5.parse(String(level).trim()
.replaceAll(', ,', ',')
.replaceAll('}\n', '},\n')
.replaceAll('},\n\t]', '}\n\t]')
.replaceAll(', },', ' },')
.replaceAll(', }', ' }')
.replaceAll('\n', '')
.replaceAll('}\n', '},\n'));
}
}

0 comments on commit 1317313

Please sign in to comment.