-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-new-format.js
47 lines (42 loc) · 1.6 KB
/
parse-new-format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let fs = require('fs');
function parseManifest( file ) {
let data = JSON.parse( fs.readFileSync( file, { encoding: 'utf-8' } ) ),
map = {};
for ( let entry of data.List ) {
map[ entry.Id ] = entry.Description;
}
return map;
}
let candidates = parseManifest( './CandidateManifest.json' ),
contests = parseManifest( './ContestManifest.json' ),
precincts = parseManifest( './PrecinctPortionManifest.json' ),
countingGroups = parseManifest( './CountingGroupManifest.json' ),
ballotTypes = parseManifest( './BallotTypeManifest.json' ),
cvrData = JSON.parse( fs.readFileSync( './CvrExport.json', { encoding: 'utf-8' } ) );
function ranksToArray( marks ) {
let arr = [];
for ( let mark of marks ) {
if ( arr[ mark.Rank - 1 ] === undefined ) {
arr[ mark.Rank - 1 ] = mark;
} else if ( Array.isArray( arr[ mark.Rank - 1 ] ) ) {
arr[ mark.Rank - 1 ] = [ ...arr[ mark.Rank - 1 ], mark ];
} else {
arr[ mark.Rank - 1 ] = [ arr[ mark.Rank - 1 ], mark ];
}
}
return arr;
}
let reformatted = cvrData.Sessions.map( function ( session ) {
let ballot = session.Modified || session.Original;
let data = {
countingGroup: countingGroups[ session.CountingGroupId ],
precinct: precincts[ ballot.PrecinctPortionId ],
ballotType: ballotTypes[ ballot.BallotTypeId ]
};
for ( let contest of ballot.Contests ) {
data[ contests[ contest.Id ] ] = ranksToArray( contest.Marks.filter( (m) => !m.IsAmbiguous ) )
.map( (m) => Array.isArray( m ) ? m.map( (n) => n ? candidates[ n.CandidateId ] : null ) : m ? candidates[ m.CandidateId ] : null );
}
return data;
} );
process.stdout.write( JSON.stringify( reformatted ) );