Created
January 24, 2020 20:13
-
-
Save arnesacnussem/fd913648c7d0861484cb8ab2abdd356d to your computer and use it in GitHub Desktop.
A json compare tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getNode = (obj, path) => { | |
return path.split('.').reduce((o, k) => { | |
return o && o[k]; | |
}, obj); | |
} | |
const fs = require("fs"); | |
var ProgressBar = require('./ProgressBar'); | |
const lang1 = 'en'; | |
const lang2 = 'zh-cht' | |
const data1 = JSON.parse(fs.readFileSync(`${lang1}.json`)); | |
const data2 = JSON.parse(fs.readFileSync(`${lang2}.json`)); | |
console.log(`data loaded`); | |
let pb = new ProgressBar(); | |
let differents = []; | |
let counter = 0, diffs = 0;; | |
console.log("diffs | count | path"); | |
function fun(obj, path = []) { | |
const base_path = path.join('.'); | |
pb.render(diffs, counter++, base_path); | |
// if (Object.keys(obj).length === 0) { | |
// console.log("Edge node,this should not happen!"); | |
// return; | |
// } | |
for (let key in obj) { | |
//if is edge node | |
if (typeof (obj[key]) != 'string' && Object.keys(obj[key]).length != 0) { | |
fun(obj[key], path.concat([key])); | |
} | |
//start compare between two lang | |
let jsonPath = `${base_path}.${key}`; | |
const e1 = String(getNode(data1, jsonPath)).trim(); | |
const e2 = String(getNode(data2, jsonPath)).trim(); | |
if (e1 !== e2) { | |
// console.log(`e1:${e1}\ne2:${e2}\n`); | |
pb.render(diffs++, counter, base_path) | |
differents.push({ path:jsonPath,a: e1, b: e2 }); | |
} | |
} | |
} | |
fun(data1); | |
const output = JSON.stringify(differents, null, 2); | |
fs.writeFile('output.json', output, () => { console.log('output write.') }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slog = require('single-line-log').stdout; | |
function ProgressBar(description) { | |
this.render = function (diffs, count, path) { | |
slog(`${diffs} | ${count} | ${path}`); | |
}; | |
} | |
module.exports = ProgressBar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment