-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcombine.js
More file actions
68 lines (57 loc) · 1.61 KB
/
Copy pathcombine.js
File metadata and controls
68 lines (57 loc) · 1.61 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// takes a flat array of Sunlight floorUpdates and a flat array of NYT votes
// returns the floorUpdates with .vote properties of the matching NYT vote, if any
var combineFloorUpdatesAndVotes = function ( floorUpdates, votes ) {
var results = floorUpdates.slice();
return results.map( function ( item ) {
var vote = findMatchingVote( item, votes );
item.vote = vote;
return item;
});
};
var findMatchingVote = function ( floorUpdate, votes ) {
if ( !floorUpdate.roll_ids.length ) {
return null;
}
var matchObj = sunlightRollToNytObj( floorUpdate.roll_ids[0] );
return findWhere( matchObj, votes );
};
var sunlightRollToNytObj = function ( rollId ) {
rollId = rollId.split( '-' );
var year = rollId[1];
var roll = numbersFromString( rollId[0] );
var congress = yearToCongress( year )
var session;
if ( Number( year ) % 2 === 0 ) {
session = 2;
} else {
session = 1;
}
return {
session: session,
congress: congress,
roll_call: roll
};
};
var numbersFromString = function ( str ) {
return str.match( /\d/g ).join( '' );
};
var findWhere = function ( obj, arr ) {
for ( var i = 0; i < arr.length; i++ ) {
if ( matches( obj, arr[i] ) ) {
return arr[i];
}
}
return null;
};
// objB is the "bigger" object
var matches = function ( objA, objB ) {
return Object.keys( objA ).every( function ( key ) {
return objA[key] == objB[key];
});
};
var yearToCongress = function ( year ) {
return Math.floor( 1 + ( year - 1789 ) / 2 );
};
var congressToYear = function ( congress, session ) {
return ( ( congress - 1 ) * 2 ) + 1789 + ( session - 1 );
};