Various Candidates helper functions.
Examples
import { Candidates } from 'election-utils'
Marge candidates
and lookupCandidates
by adding
isMainAndRunning
to each candidate.
Parameters
$0.candidates
Array an array of Candidates$1.lookupCandidates
Array an array of lookup candidates_ref
Examples
import { Candidates, primaries2016Candidates } from 'election-utils'
Candidates.addIsMainAndRunning({
candidates,
lookupCandidates: primaries2016Candidates
})[0] //=> { isMainAndRunning: true, ... }
Returns Array a new array of items augmented with isMainAndRunning
Get the candidates' total vote count.
Parameters
candidates
Array an array of Candidates
Examples
Candidates.getVoteCount(myCandidates) //=> 123
Returns Number the total vote count
Map candidates to color classes. Useful for coloring choropleths.
Parameters
$0.candidates
Array an array of Candidates$1.party
String the Candidates' party$2.mainCandidates
Array an array of main Candidates_ref2
Examples
const m = Candidates.makeColorMappings({ candidates, party, mainCandidates })
m[0] //=> { colorClass: 'democratic-0', candidateID: '123' }
Returns Array an array of objects containing colorClass
and candidateID
Sort candidates by vote count and ballot order, in that order.
Parameters
candidates
Array an array of Candidates
Examples
Candidates.sort(myCandidates) //=> mySortedCandidates
Returns Array a new array of Candidates, sorted. Does not mutate original array.
Various Candidate helper functions.
Examples
import { Candidate } from 'election-utils'
Determine if this candidate is the winner.
Will respect winnerOverride
if present.
Parameters
candidate
Object the candidate
Examples
Candidate.isWinner(winningCandidate) //=> true
Returns Boolean whether candidate is winner
Provides various functions to standardize AP data.
Examples
import { standardize } from 'election-utils'
Collapses a party name to its upper-case abbreviation.
Parameters
name
String party name. Case insensitive.
Examples
standardize.collapseParty('democratic') //=> 'DEM'
Returns String the party's upper-case abbreviation.
Collapses a state name to its upper-case abbreviation.
Parameters
name
String state name. Case insensitive.
Examples
standardize.collapseState('iowa') //=> 'IA'
Returns String the state's upper-case abbreviation.
Expands a party abbreviation to its title-case name.
Parameters
abbr
String party abbreviation. Case insensitive.
Examples
standardize.expandParty('dem') //=> 'Democratic'
Returns String the party's title-case name.
Expands a state abbreviation to its title-case name.
Parameters
abbr
String state abbreviation. Case insensitive.
Examples
standardize.expandState('ia') //=> 'Iowa'
Returns String the state's title-case name.
Convert race type to its appropriate name.
Set invert
to true
to convert back to what AP gives us.
Parameters
rType
String the race type, e.g. 'Caucuses'.invert
Boolean whether to invert back to what AP gives us.
Examples
standardize.raceType('Caucus') //=> 'Caucuses'
standardize.raceType('Primary') //=> 'Primary'
standardize.raceType('Caucuses', true) //=> 'Caucus'
Returns String the converted race type.
Format timestamp.
Useful for displaying last updated
.
Parameters
$0.timestamp
String a timestamp_ref
Examples
import { formatTimestamp } from 'election-utils'
formatTimestamp(results) //=> 'Mar. 1, 2:26 PM EST'
Returns String the formatted timestamp
An array of parties, each with an abbr
and name
property.
Examples
import { parties } from 'election-utils'
parties[0] //=> { abbr: 'DEM', name: 'Democratic' }
An array of 2016 primaries candidates, each with first
, last
, party
and suspendedDate
properties.
Examples
import { primaries2016Candidates } from 'election-utils'
primaries2016Candidates[0] //=> { 'last': 'bush', 'first': 'jeb', 'party': 'republican' ...}
An array of 2016 (AP tabulated) primaries dates, each with stateAbbr
, party
, date
, raceType
, and resultsNote
properties.
Only contains state presidential primaries.
Examples
import { primaries2016Dates } from 'election-utils'
primaries2016Dates[0] //=> { stateAbbr: 'IA', party: 'Democratic', date: '2016-02-01', ... }
An array of race types.
Examples
import { raceTypes } from 'election-utils'
raceTypes[0] //=> 'Caucuses'