Created
July 27, 2018 21:00
-
-
Save sammorrisdesign-zz/d5bbf7c30d54467d9d92e8016382c39b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var csv = require('csvtojson'); | |
var jsonexport = require('jsonexport'); | |
var data = fs.readFileSync('data.csv', 'utf8'); | |
// create a new data set | |
var organisedData = []; | |
// load the unorganised csv file | |
csv().fromString(data).then(function(json) { | |
// loop through every row | |
for (var row in json) { | |
// add first item to organisedData | |
if (organisedData.length === 0) { | |
pushData(json[0]); | |
} | |
// check if the case the row is referring to is already in our organisedData set. | |
var caseExists = false; | |
for (var party in organisedData) { | |
if (json[row].case_number == organisedData[party].case_number) { | |
caseExists = party; | |
} | |
} | |
if (caseExists) { | |
// a case already exists, add data to existing case in organisedData | |
pushData(json[row], caseExists); | |
} else { | |
// this is a new case, create a new case in organisedData | |
pushData(json[row]); | |
} | |
} | |
// the data is now organised. Let's save it out as a new CSV file | |
jsonexport(organisedData, function(err, csv) { | |
fs.writeFileSync('organisedData.csv', csv); | |
}) | |
}); | |
// this part of the code deals with adding and appending to cases | |
function pushData(row, partyNumber = null) { | |
// finds out the role of a party member | |
var role = row.party_role.replace(/ /g, '_').toLowerCase(); | |
// we have to deal with creating new cases and appened to existing cases differently | |
if (partyNumber) { | |
// checks to see if someone with the same job exists on this case and appends a relevant number | |
if (organisedData[partyNumber][role + '_name']) { | |
for (var i = 1; 6 > i; i++) { | |
if (!organisedData[partyNumber][role + '_name_' + i]) { | |
role = role + '_' + i; | |
break; | |
} | |
} | |
} | |
// adds new member to existing case | |
organisedData[partyNumber][role + '_name'] = row.party_name; | |
organisedData[partyNumber][role + '_email'] = row.party_email; | |
organisedData[partyNumber][role + '_phone'] = row.party_phone; | |
organisedData[partyNumber][role + '_firm'] = row.party_firm; | |
} else { | |
// creates a new case with basic information | |
var newCase = {}; | |
newCase.case_number = row.case_number; | |
newCase.court = row.court | |
newCase.case_name = row.case_name; | |
// and adds the first party member | |
newCase[role + '_name'] = row.party_name; | |
newCase[role + '_email'] = row.party_email; | |
newCase[role + '_phone'] = row.party_phone; | |
newCase[role + '_firm'] = row.party_firm; | |
// add the new case to the organisedData | |
organisedData.push(newCase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment