Globals

Property

Property

constant

stateNameHash

Example

stateNameHash['01']
// returns 'Alabama'

Parameter

Name Type Optional Description

stateFips

string

 

The state fips code.

Throws

Error 

  • If the state fips code is invalid.
Returns

string 

  • The state name

Methods

boundariesAvailableForRaceType(raceType) → array

Examples

boundariesAvailableForRaceType('president')
// returns ['state', 'county']
boundariesAvailableForRaceType('senate')
// returns ['state']
boundariesAvailableForRaceType('house')
// returns ['district']
boundariesAvailableForRaceType(2016)
// returns null

Parameter

Name Type Optional Description

raceType

string

 

Returns

array 

  • An array of the available district types

candidateVotePercentage(candidateVote, totalVotes) → number

Given the absolute number of votes a candidate has received, and the total number of votes in the election, returns the percentage of votes the candidate has received.

Example

getPercentageOfVotes(100, 200)
// returns 50

Parameters

Name Type Optional Description

candidateVote

number

 

The number of votes the candidate has received.

totalVotes

number

 

The total number of votes in the election.

Returns

number 

  • The percentage of votes the candidate has received.

getStateAbbrFromStateFips(stateFips) → string

Examples

getStateAbbrFromStateFips('01')
// returns 'AL'
getStateAbbrFromStateFips('36')
// returns 'NY'
getStateAbbrFromStateFips('XX')
// throws an error

Parameter

Name Type Optional Description

stateFips

string

 

The state fips code.

Throws

Error 

  • If the state fips code is invalid.
Returns

string 

  • The state abbreviation

getStateCodeFromCountyFips(countyFips) → string

Get the state code from the county fips string

Examples

getStateCodeFromCountyFips('01001')
// returns '01'
getStateCodeFromCountyFips(01000)
// throws Error
getStateCodeFromCountyFips('01')
// throws Error

Parameter

Name Type Optional Description

countyFips

string

 

The county fips code.

Returns

string 

  • The state fips code.

getStateFipsFromStateAbbr(stateAbbr) → string

Example

getStateFipsFromStateAbbr('CA')
// => '06'
getStateFipsFromStateAbbr('NY')
// => '36'

Parameter

Name Type Optional Description

stateAbbr

string

 

Returns

string 

  • The fips code for the state

isBoundaryAvailableForRaceType(raceType, boundaryType)

Examples

isBoundaryAvailableForRaceType('president', 'county')
// returns true
isBoundaryAvailableForRaceType('president', 'state')
// returns true
isBoundaryAvailableForRaceType('president', 'district')
// returns false

Parameters

Name Type Optional Description

raceType

string

 

The race type, like 'president', 'house', or 'senate'

boundaryType

string

 

The type of boundary, like 'county', 'state', or 'district'

sortCandidatesByVotes(candidates, sortFunction) → Array

Given an array of candidate objects, returns a sorted array of candidate objects, sorted by the number of votes they have received with the specified sort function.

Parameters

Name Type Optional Description

candidates

Array

 

An array of candidate objects.

sortFunction

function()

 

The function to use to sort the candidates (like d3.descending)

Throws

Error 

  • If the candidates array is invalid.
Returns

Array 

  • A sorted array of candidate objects.

stateAbbrToFips(stateAbbreviation) → string

Get the state fips code from the abbreviation, like 'NY' to '36'

Example

getStateFipsFromAbbreviation('NY')
// returns '36'

Parameter

Name Type Optional Description

stateAbbreviation

string

 

The state abbreviation.

Returns

string 

  • The state fips code.

stateAbbrToName(stateAbbr) → string

Example

getStateNameFromStateAbbr('AL')
// returns 'Alabama'

Parameter

Name Type Optional Description

stateAbbr

string

 

Two letter state abbreviation

Throws

Error 

  • If the state abbreviation is invalid.
Returns

string 

  • The state name

stateFipsToName(stateFips) → string

Example

stateFipsToName('01')
// returns 'Alabama'

Parameter

Name Type Optional Description

stateFips

string

 

Throws

Error 

  • If the state fips code is invalid.
Returns

string 

  • The state name

stateNameToFips(stateName) → string

Example

getStateFipsFromStateName('Alabama')
// returns '01'

Parameter

Name Type Optional Description

stateName

string

 

Throws

Error 

  • If the state name is invalid.
Returns

string 

  • The state fips code