Skip to content

Instantly share code, notes, and snippets.

@doug-wade
Created April 5, 2022 21:58
Show Gist options
  • Save doug-wade/752ff2ab7a4b012a3ee3eacb24336f84 to your computer and use it in GitHub Desktop.
Save doug-wade/752ff2ab7a4b012a3ee3eacb24336f84 to your computer and use it in GitHub Desktop.
Remove a set of block-listed keys from a json file
const fs = require('fs')
const path = require('path')
const inputDirectory = process.argv[process.argv.indexOf('-i') + 1]
const outputDirectory = process.argv[process.argv.indexOf('-o') + 1]
const BLOCK_LIST = new Set(['type', 'description', 'extensions'])
if (!fs.existsSync(outputDirectory)){
fs.mkdirSync(outputDirectory);
}
const filenames = fs.readdirSync(inputDirectory);
filenames.forEach(filename => {
sanitizeFile(filename)
})
function sanitizeFile(filename) {
const filePath = path.resolve(`${inputDirectory}/${filename}`)
const file = fs.readFileSync(filePath)
const json = JSON.parse(file)
const result = sanitize(json)
fs.writeFileSync(`${outputDirectory}/${filename}`, JSON.stringify(result, null, 2))
}
function sanitize(obj) {
Object.keys(obj).forEach(key => {
if (BLOCK_LIST.has(key.toLowerCase())) {
obj[key] = undefined;
} else if (typeof obj[key] === 'object') {
obj[key] = sanitize(obj[key]);
}
});
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment