Skip to content

Instantly share code, notes, and snippets.

@simlu
Created August 22, 2018 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simlu/9cf4f661b5f06539e6280893f2613ff5 to your computer and use it in GitHub Desktop.
Save simlu/9cf4f661b5f06539e6280893f2613ff5 to your computer and use it in GitHub Desktop.
Make files unique, ignoring different string / number value in object hierarchy
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