Skip to content

Instantly share code, notes, and snippets.

@jadeallencook
Last active November 28, 2017 20:23
Show Gist options
  • Save jadeallencook/89438b279ec6f3f85beaa12f2d4d4ae2 to your computer and use it in GitHub Desktop.
Save jadeallencook/89438b279ec6f3f85beaa12f2d4d4ae2 to your computer and use it in GitHub Desktop.
Formats exported JSON from Google Form to newspaper format.
function exportHomicideReport(reports) {
return new Promise((complete, error) => {
// cache build info
let output = '',
outputPromises = [],
monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
// cache google info
const googleURL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=',
googleAPI = '&key=AIzaSyCXllb7HBvBT_nv5jF0dxjaRAo4T6D34xw';
// loop over each report
for (let report of reports) {
// get location from coordinates
outputPromises.push(new Promise((resolve, reject) => {
const coordinates = report.location.replace(' ', ''),
requestURL = googleURL + coordinates + googleAPI;
$.getJSON(requestURL, (geoData) => {
const city = geoData.results[0]['address_components'][3]['long_name'],
date = new Date(report.date),
month = monthNames[date.getMonth()],
commaPosition = report.summary.indexOf(',');
output += city.toUpperCase() + ', ';
output += month + '. ' + date.getDate() + ' — ';
output += '<b>' + [report.summary.slice(0, commaPosition), '</b>', report.summary.slice(commaPosition)].join('');
output += '<b> Method: </b>' + report.type;
output += '<br /><br />';
resolve();
});
}));
}
Promise.all(outputPromises).then(() => {
complete(output);
});
});
}
// test output
exportHomicideReport(data).then((output) => {
console.log(output);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment