Globals

Property

Abstract type

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) → string

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

candidateVotePercentage(100, 200)
// returns '50.0'

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

string 

  • The percentage of votes the candidate has received, formatted to one decimal place.

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'

stateAbbrToFips(stateAbbr) → string

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

Example

stateAbbrToFips('NY')
// returns '36'

Parameter

Name Type Optional Description

stateAbbr

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

Abstract type

Candidate  Object

Sort an array of candidate objects by vote count.

Each candidate must contain a candidatevotes property that can be coerced to a number. Any extra properties are preserved.

By default the list is sorted descending (highest → lowest). Pass a custom sortFunction if you need a different order – its contract is identical to Array.prototype.sort.

Examples

// Default (descending)
sortCandidatesByVotes(candidates)
// Ascending order
sortCandidatesByVotes(candidates, (a, b) => a - b)
// Custom tie-breaker: descending votes then alphabetical by name
sortCandidatesByVotes(candidates, (a, b) => {
  const diff = b - a
  return diff !== 0 ? diff : a.name.localeCompare(b.name)
})

Parameters

Name Type Optional Description

raceCandidateArray

Array of Candidate

 

– Candidates to sort (mutated in-place).

sortFunction

function(number, number)

Yes

– Optional compare function. Defaults to (a, b) => b - a for descending order.

Properties

Name Type Optional Description

candidatevotes

(number or string)

 

– Vote total for the candidate.

*

any

Yes

– Additional properties are allowed and untouched.

Throws

TypeError 

If raceCandidateArray is not a valid array.

Returns

Array of Candidate 

The same array instance, ordered according to the compare logic.