user
Create a Node.js script that takes an input JSON file containing an array of objects with properties "id", "title", and "description". The script should filter out any objects where the title or description contains profanity, then save the filtered results as a new JSON file.
PLAINFORMAT
assistant
const fs = require('fs');
const path = require('path');
const BadWordsFilter = require('bad-words');

// Read input JSON file
const inputFile = process.argv[2];
if (!inputFile) {
  console.error('Please provide an input JSON file as argument.');
  process.exit(1);
}

const inputFilePath = path.resolve(inputFile);

fs.readFile(inputFilePath, 'utf8', (err, data) => {
  if (err) {
    console.error(`Error reading file: ${err}`);
    process.exit(1);
  }

  // Parse JSON data
  let jsonData;
  try {
    jsonData = JSON.parse(data);
  } catch (error) {
    console.error(`Error parsing JSON data: ${error}`);
    process.exit(1);
  }

  // Filter out objects with profanity
  const filter = new BadWordsFilter();
  const filteredData = jsonData.filter(item =>
    !filter.isProfane(item.title) && !filter.isProfane(item.description)
  );

  // Save filtered data to a new JSON file
  const outputFile = process.argv[3] || 'filtered_output.json';
  const outputFilePath = path.resolve(outputFile);

  fs.writeFile(outputFilePath, JSON.stringify(filteredData, null, 2), err => {
    if (err) {
      console.error(`Error writing output file: ${err}`);
      process.exit(1);
    }
    console.log(`Filtered data saved to ${outputFilePath}`);
  });
});