election-helpers
v0.0.8
Published
Election helpers
Readme
Election Helpers
Read docs.md or GH pages for documentation
Testing
The project uses Vitest for testing. Each utility function has multiple test cases to ensure reliability.
To run tests:
# Run tests once
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverageTest coverage reports are automatically generated and uploaded to Codecov on each push.
--
Functions
stateNameHash ⇒ string
Example
stateNameHash['01']
// returns 'Alabama'getStateFipsFromStateAbbr(stateAbbr) ⇒ string
Example
getStateFipsFromStateAbbr('CA')
// => '06'
getStateFipsFromStateAbbr('NY')
// => '36'stateAbbrToName(stateAbbr) ⇒ string
Example
getStateNameFromStateAbbr('AL')
// returns 'Alabama'getStateAbbrFromStateFips(stateFips) ⇒ string
Example
getStateAbbrFromStateFips('01')
// returns 'AL'Example
getStateAbbrFromStateFips('36')
// returns 'NY'Example
getStateAbbrFromStateFips('XX')
// throws an errorgetStateCodeFromCountyFips(countyFips) ⇒ string
Example
getStateCodeFromCountyFips('01001')
// returns '01'Example
getStateCodeFromCountyFips(01000)
// throws ErrorExample
getStateCodeFromCountyFips('01')
// throws ErrorcandidateVotePercentage(candidateVote, totalVotes) ⇒ number
Example
getPercentageOfVotes(100, 200)
// returns 50sortCandidatesByVotes(candidates, sortFunction) ⇒ Array
stateFipsToName(stateFips) ⇒ string
Example
stateFipsToName('01')
// returns 'Alabama'stateAbbrToFips(stateAbbreviation) ⇒ string
Example
getStateFipsFromAbbreviation('NY')
// returns '36'stateNameToFips(stateName) ⇒ string
Example
getStateFipsFromStateName('Alabama')
// returns '01'boundariesAvailableForRaceType(raceType) ⇒ array
Example
boundariesAvailableForRaceType('president')
// returns ['state', 'county']Example
boundariesAvailableForRaceType('senate')
// returns ['state']Example
boundariesAvailableForRaceType('house')
// returns ['district']Example
boundariesAvailableForRaceType(2016)
// returns nullisBoundaryAvailableForRaceType(raceType, boundaryType)
Example
isBoundaryAvailableForRaceType('president', 'county')
// returns trueExample
isBoundaryAvailableForRaceType('president', 'state')
// returns trueExample
isBoundaryAvailableForRaceType('president', 'district')
// returns false