Skip to content

Instantly share code, notes, and snippets.

@ilagnev
Created December 13, 2017 22:00
Show Gist options
  • Save ilagnev/2c6f02caae6a29b44db4b736936c7f2f to your computer and use it in GitHub Desktop.
Save ilagnev/2c6f02caae6a29b44db4b736936c7f2f to your computer and use it in GitHub Desktop.
Read json, add hash uid, save json
const crypto = require('crypto');
const fs = require('fs');
const hash = data => crypto.createHash('sha1').update(data).digest('hex');
const usedHashes = [];
fs.readFile('config.json', (err, data) => {
const json = JSON.parse(data.toString());
// console.log(json);
addUid(json);
});
function addUid(config) {
Object.keys(config).forEach(dis => {
config[dis].forEach((account, i) => {
// console.log(account);
// account.uid = hash(account.title);
// config[dis][i] = Object.assign({uid: hash(account.title)}, account);
const hs = hash(account.title);
if (usedHashes.indexOf(hs) > -1)
throw new Error('collision detected');
usedHashes.push(hs);
config[dis][i] = {uid: hs, ...account};
})
})
// console.log(config);
saveConfig(config);
}
function saveConfig(config) {
fs.writeFile('hashed-config.json', JSON.stringify(config), (err)=>{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment