Skip to content

Latest commit

 

History

History
270 lines (166 loc) · 7.49 KB

doc.md

File metadata and controls

270 lines (166 loc) · 7.49 KB

Candidates

Various Candidates helper functions.

Examples

import { Candidates } from 'election-utils'

addIsMainAndRunning

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

getVoteCount

Get the candidates' total vote count.

Parameters

  • candidates Array an array of Candidates

Examples

Candidates.getVoteCount(myCandidates) //=> 123

Returns Number the total vote count

makeColorMappings

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

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.

Candidate

Various Candidate helper functions.

Examples

import { Candidate } from 'election-utils'

isWinner

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

standardize

Provides various functions to standardize AP data.

Examples

import { standardize } from 'election-utils'

collapseParty

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.

collapseState

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.

expandParty

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.

expandState

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.

raceType

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.

formatTimestamp

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

parties

An array of parties, each with an abbr and name property.

Examples

import { parties } from 'election-utils'
parties[0] //=> { abbr: 'DEM', name: 'Democratic' }

primaries2016Candidates

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' ...}

primaries2016Dates

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', ... }

raceTypes

An array of race types.

Examples

import { raceTypes } from 'election-utils'
raceTypes[0] //=> 'Caucuses'