Skip to content

Instantly share code, notes, and snippets.

@stanographer
Last active February 17, 2019 04:35
Show Gist options
  • Save stanographer/06458dfbc64c5fa4cff73238b9c1cc7e to your computer and use it in GitHub Desktop.
Save stanographer/06458dfbc64c5fa4cff73238b9c1cc7e to your computer and use it in GitHub Desktop.
Convert a ginormous table of corrections data.
const fs = require('fs');
const Json2csvParser = require('json2csv').Parser;
let output;
// Read in a giant CSV file.
fs.readFile('input.json', 'utf8', (err, data) => {
if (err) throw err;
output = JSON.parse(data);
parse(output);
});
// Returns a new array deleting the unnecessary elements.
function parse(items) {
const filtered = items.map(x => {
delete x['DateOut'];
delete x['housing_status'];
return x;
});
countIncidents(filtered);
}
// Count the number of incidents per de-identified inmate ID.
function countIncidents(arr) {
let map = {};
// Loops over the rows and matching by ID.
// if there's a repeat in the ID, it increases a counter
// rather than creating a new row for the inmate/incident.
for (let i = 0; i < arr.length; i++) {
// If no previous entry, then assign them to the
// map by ID.
if (map[arr[i]['ID']] === undefined) {
map[arr[i]['ID']] = {
count: 1,
data: arr[i]
};
} else {
// If that particular ID has already been encountered,
// instead, increase counter to append to column labels
// and append properties to the original inmate/incident object.
map[arr[i]['ID']]['count'] += 1;
let count = map[arr[i]['ID']]['count'];
map[arr[i]['ID']]['data'] = {
...map[arr[i]['ID']]['data'],
['DateIn' + count]: arr[i]['DateIn'],
['facility' + count]: arr[i]['facility'],
['DaysInRhu' + count]: arr[i]['DaysInRhu']
};
}
}
return stripExtras(map);
}
// Recursion === max call stack limit :\
// function countIncidents(arr, map = {}) {
// if (arr.length !== 0) {
// const incident = arr.shift();
// if (map[incident['ID']] === undefined) {
// map[incident['ID']] = {
// count: 1,
// data: incident
// };
// return countIncidents(arr, map);
// } else {
// map[incident['ID']]['count'] += 1;
// let count = map[incident['ID']]['count'];
// map[incident['ID']]['data'] = {
// ...map[incident['ID']]['data'],
// ['DateIn' + count]: incident['DateIn'],
// ['facility' + count]: incident['facility'],
// ['DaysInRhu' + count]: incident['DaysInRhu']
// };
// }
// return countIncidents(arr, map);
// }
// return stripExtras(map);
// }
// Goes through all the newly returned objects and
// removes the "count" property and just passes
// over the contents of "data."
function stripExtras(ob) {
const output = [];
Object.keys(ob).forEach(key => {
output.push(ob[key].data);
});
return csvify(output);
}
// function jsonify(input) {
// fs.writeFileSync('./output.json', JSON.stringify(input));
// console.log('All done!');
// }
// Uses Json2csv to convert resulting json file into CSV.
// Then save the resulting data to the directory using fs.
function csvify(json) {
const transformOpts = { highWaterMark: 16384, encoding: 'utf-8' };
try {
const parser = new Json2csvParser(transformOpts);
const csv = parser.parse(json);
fs.writeFileSync('./output.csv', csv);
console.log('Conversion complete, fucker.');
} catch (err) {
console.error(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment