Created
August 22, 2018 00:55
-
-
Save simlu/9cf4f661b5f06539e6280893f2613ff5 to your computer and use it in GitHub Desktop.
Make files unique, ignoring different string / number value in object hierarchy
This file contains 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 fs = require("fs"); | |
const path = require("path"); | |
const get = require("lodash.get"); | |
const set = require("lodash.set"); | |
const objectScan = require("object-scan"); | |
const objectHash = require("object-hash"); | |
const data = Object.entries(fs.readdirSync(path.join(__dirname, "query")) | |
.map(f => f.slice(0, -5)) | |
.reduce((p, f) => Object.assign(p, { | |
// eslint-disable-next-line import/no-dynamic-require, global-require | |
[f]: require(`./query/${f}`) | |
}), {})); | |
data.forEach(([k, v]) => { | |
const hash = JSON.parse(JSON.stringify(v)); | |
objectScan(["**"], { | |
filterFn: (key, value) => ["number", "string"].includes(typeof value), | |
joined: false | |
})(v) | |
.forEach(p => set(hash, p, typeof get(hash, p))); | |
fs.writeFileSync(path.join(__dirname, "out", `${k.split("-")[0]}-${objectHash(hash)}.json`), JSON.stringify(v, null, 2), 'utf8'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment