election-utils
Advanced tools
Comparing version 3.1.0 to 3.2.0
import orderBy from 'lodash.orderby'; | ||
import unitedStates from 'united-states'; | ||
var babelHelpers = {}; | ||
babelHelpers.extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
babelHelpers; | ||
/** | ||
@@ -865,5 +883,60 @@ * An array of 2016 primaries candidates, each with `first`, `last`, `party` and `suspendedDate` properties. | ||
/** | ||
* Map candidates to color classes. | ||
* Useful for coloring choropleths. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {String} $1.party the Candidates' party | ||
* @param {Array} $2.mainCandidates an array of main Candidates | ||
* @returns {Array} an array of objects containing `colorClass` and `candidateID` | ||
* @example | ||
* const m = Candidates.makeColorMappings({ candidates, party, mainCandidates }) | ||
* m[0] //=> { colorClass: 'democratic-0', candidateID: '123' } | ||
*/ | ||
makeColorMappings: function makeColorMappings(_ref) { | ||
var candidates = _ref.candidates; | ||
var party = _ref.party; | ||
var mainCandidates = _ref.mainCandidates; | ||
// this will take a list of state-level candidates. | ||
// we then sort the list by main status, vote count and ballot order. | ||
// we then assign color classes based on this. | ||
// finally we return a hash, { candidateID: 123, colorClass: 'red' } | ||
// get lowercase party - we'll use it quite a bit | ||
var partyL = party.toLowerCase(); | ||
// get a list of this party's candidate last names | ||
var partyCandidatesNames = mainCandidates.filter(function (x) { | ||
return x.party === partyL; | ||
}).map(function (x) { | ||
return x.last; | ||
}); | ||
// add 'mainCandidate' boolean flag | ||
var enhancedCandidates = candidates.map(function (c) { | ||
return babelHelpers.extends({}, c, { | ||
mainCandidate: partyCandidatesNames.indexOf(c.last.toLowerCase()) > -1 | ||
}); | ||
}); | ||
// sort by main, votecount, ballotorder (in that order | ||
var mapping = orderBy(enhancedCandidates, ['mainCandidate', 'voteCount', 'ballotOrder'], ['desc', 'desc', 'asc']) | ||
// and return a candidate/color class hash | ||
.map(function (c, i) { | ||
return { | ||
colorClass: c.mainCandidate ? partyL + '-' + i : partyL + '-other', | ||
candidateID: c.candidateID | ||
}; | ||
}); | ||
return mapping; | ||
}, | ||
/** | ||
* Get the candidates' total vote count. | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Number} the total vote count | ||
@@ -884,3 +957,3 @@ * @example | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Array} a new array of Candidates, sorted. Does not mutate original array. | ||
@@ -887,0 +960,0 @@ * @example |
@@ -8,2 +8,20 @@ 'use strict'; | ||
var babelHelpers = {}; | ||
babelHelpers.extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
babelHelpers; | ||
/** | ||
@@ -870,5 +888,60 @@ * An array of 2016 primaries candidates, each with `first`, `last`, `party` and `suspendedDate` properties. | ||
/** | ||
* Map candidates to color classes. | ||
* Useful for coloring choropleths. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {String} $1.party the Candidates' party | ||
* @param {Array} $2.mainCandidates an array of main Candidates | ||
* @returns {Array} an array of objects containing `colorClass` and `candidateID` | ||
* @example | ||
* const m = Candidates.makeColorMappings({ candidates, party, mainCandidates }) | ||
* m[0] //=> { colorClass: 'democratic-0', candidateID: '123' } | ||
*/ | ||
makeColorMappings: function makeColorMappings(_ref) { | ||
var candidates = _ref.candidates; | ||
var party = _ref.party; | ||
var mainCandidates = _ref.mainCandidates; | ||
// this will take a list of state-level candidates. | ||
// we then sort the list by main status, vote count and ballot order. | ||
// we then assign color classes based on this. | ||
// finally we return a hash, { candidateID: 123, colorClass: 'red' } | ||
// get lowercase party - we'll use it quite a bit | ||
var partyL = party.toLowerCase(); | ||
// get a list of this party's candidate last names | ||
var partyCandidatesNames = mainCandidates.filter(function (x) { | ||
return x.party === partyL; | ||
}).map(function (x) { | ||
return x.last; | ||
}); | ||
// add 'mainCandidate' boolean flag | ||
var enhancedCandidates = candidates.map(function (c) { | ||
return babelHelpers.extends({}, c, { | ||
mainCandidate: partyCandidatesNames.indexOf(c.last.toLowerCase()) > -1 | ||
}); | ||
}); | ||
// sort by main, votecount, ballotorder (in that order | ||
var mapping = orderBy(enhancedCandidates, ['mainCandidate', 'voteCount', 'ballotOrder'], ['desc', 'desc', 'asc']) | ||
// and return a candidate/color class hash | ||
.map(function (c, i) { | ||
return { | ||
colorClass: c.mainCandidate ? partyL + '-' + i : partyL + '-other', | ||
candidateID: c.candidateID | ||
}; | ||
}); | ||
return mapping; | ||
}, | ||
/** | ||
* Get the candidates' total vote count. | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Number} the total vote count | ||
@@ -889,3 +962,3 @@ * @example | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Array} a new array of Candidates, sorted. Does not mutate original array. | ||
@@ -892,0 +965,0 @@ * @example |
{ | ||
"name": "election-utils", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "This module provides various utility functions for dealing with AP election data.", | ||
@@ -26,4 +26,7 @@ "main": "build/index.js", | ||
"babel-cli": "^6.5.1", | ||
"babel-plugin-syntax-object-rest-spread": "^6.5.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.5.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-preset-es2015-rollup": "^1.0.0", | ||
"babel-preset-stage-1": "^6.5.0", | ||
"documentation": "^4.0.0-beta", | ||
@@ -30,0 +33,0 @@ "faucet": "0.0.1", |
@@ -12,5 +12,54 @@ import orderBy from 'lodash.orderby'; | ||
/** | ||
* Map candidates to color classes. | ||
* Useful for coloring choropleths. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {String} $1.party the Candidates' party | ||
* @param {Array} $2.mainCandidates an array of main Candidates | ||
* @returns {Array} an array of objects containing `colorClass` and `candidateID` | ||
* @example | ||
* const m = Candidates.makeColorMappings({ candidates, party, mainCandidates }) | ||
* m[0] //=> { colorClass: 'democratic-0', candidateID: '123' } | ||
*/ | ||
makeColorMappings: ({ candidates, party, mainCandidates }) => { | ||
// this will take a list of state-level candidates. | ||
// we then sort the list by main status, vote count and ballot order. | ||
// we then assign color classes based on this. | ||
// finally we return a hash, { candidateID: 123, colorClass: 'red' } | ||
// get lowercase party - we'll use it quite a bit | ||
const partyL = party.toLowerCase() | ||
// get a list of this party's candidate last names | ||
const partyCandidatesNames = mainCandidates | ||
.filter(x => x.party === partyL) | ||
.map(x => x.last) | ||
// add 'mainCandidate' boolean flag | ||
const enhancedCandidates = candidates.map(c => ({ | ||
...c, | ||
mainCandidate: partyCandidatesNames.indexOf(c.last.toLowerCase()) > -1 | ||
})) | ||
// sort by main, votecount, ballotorder (in that order | ||
const mapping = orderBy(enhancedCandidates, | ||
['mainCandidate', 'voteCount', 'ballotOrder'], | ||
['desc', 'desc', 'asc']) | ||
// and return a candidate/color class hash | ||
.map((c, i) => ({ | ||
colorClass: c.mainCandidate ? `${partyL}-${i}` : `${partyL}-other`, | ||
candidateID: c.candidateID | ||
})) | ||
return mapping | ||
}, | ||
/** | ||
* Get the candidates' total vote count. | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Number} the total vote count | ||
@@ -25,3 +74,3 @@ * @example | ||
* | ||
* @param {Object} candidates an array of Candidates | ||
* @param {Array} candidates an array of Candidates | ||
* @returns {Array} a new array of Candidates, sorted. Does not mutate original array. | ||
@@ -28,0 +77,0 @@ * @example |
Sorry, the diff of this file is not supported yet
73929
2923
14