election-utils
Advanced tools
Comparing version 3.3.2 to 3.4.0
@@ -875,2 +875,6 @@ import orderBy from 'lodash.orderby'; | ||
var compareStringsIgnoreCase = function compareStringsIgnoreCase(a, b) { | ||
return a.toUpperCase() === b.toUpperCase(); | ||
}; | ||
/** | ||
@@ -885,2 +889,44 @@ * Various Candidates helper functions. | ||
/** | ||
* Marge `candidates` and `lookupCandidates` by adding | ||
* `isMainAndRunning` to each candidate. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {Array} $1.lookupCandidates an array of lookup candidates | ||
* @returns {Array} a new array of items augmented with `isMainAndRunning` | ||
* @example | ||
* import { Candidates, primaries2016Candidates } from 'election-utils' | ||
* Candidates.addIsMainAndRunning({ | ||
* candidates, | ||
* lookupCandidates: primaries2016Candidates | ||
* })[0] //=> { isMainAndRunning: true, ... } | ||
*/ | ||
addIsMainAndRunning: function addIsMainAndRunning(_ref) { | ||
var candidates = _ref.candidates; | ||
var lookupCandidates = _ref.lookupCandidates; | ||
return candidates.map(function (c) { | ||
// try to find this candidate in lookupCandidates | ||
var mainCandidate = lookupCandidates.find(function (l) { | ||
return( | ||
// if this candidate has a first name, find its lookup counterpart | ||
(!c.first || compareStringsIgnoreCase(l.first, c.first)) && ( | ||
// if this candidate has a last name, find its lookup counterpart | ||
!c.last || compareStringsIgnoreCase(l.last, c.last)) && ( | ||
// if this candidate has a party, find its lookup counterpart | ||
!c.party || compareStringsIgnoreCase(l.party, standardize.expandParty(c.party))) | ||
); | ||
}); | ||
var isMainAndRunning = !!mainCandidate && !mainCandidate.suspendedDate; | ||
return babelHelpers.extends({}, c, { | ||
isMainAndRunning: isMainAndRunning | ||
}); | ||
}); | ||
}, | ||
/** | ||
* Map candidates to color classes. | ||
@@ -897,6 +943,6 @@ * Useful for coloring choropleths. | ||
*/ | ||
makeColorMappings: function makeColorMappings(_ref) { | ||
var candidates = _ref.candidates; | ||
var party = _ref.party; | ||
var mainCandidates = _ref.mainCandidates; | ||
makeColorMappings: function makeColorMappings(_ref2) { | ||
var candidates = _ref2.candidates; | ||
var party = _ref2.party; | ||
var mainCandidates = _ref2.mainCandidates; | ||
@@ -903,0 +949,0 @@ |
@@ -880,2 +880,6 @@ 'use strict'; | ||
var compareStringsIgnoreCase = function compareStringsIgnoreCase(a, b) { | ||
return a.toUpperCase() === b.toUpperCase(); | ||
}; | ||
/** | ||
@@ -890,2 +894,44 @@ * Various Candidates helper functions. | ||
/** | ||
* Marge `candidates` and `lookupCandidates` by adding | ||
* `isMainAndRunning` to each candidate. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {Array} $1.lookupCandidates an array of lookup candidates | ||
* @returns {Array} a new array of items augmented with `isMainAndRunning` | ||
* @example | ||
* import { Candidates, primaries2016Candidates } from 'election-utils' | ||
* Candidates.addIsMainAndRunning({ | ||
* candidates, | ||
* lookupCandidates: primaries2016Candidates | ||
* })[0] //=> { isMainAndRunning: true, ... } | ||
*/ | ||
addIsMainAndRunning: function addIsMainAndRunning(_ref) { | ||
var candidates = _ref.candidates; | ||
var lookupCandidates = _ref.lookupCandidates; | ||
return candidates.map(function (c) { | ||
// try to find this candidate in lookupCandidates | ||
var mainCandidate = lookupCandidates.find(function (l) { | ||
return( | ||
// if this candidate has a first name, find its lookup counterpart | ||
(!c.first || compareStringsIgnoreCase(l.first, c.first)) && ( | ||
// if this candidate has a last name, find its lookup counterpart | ||
!c.last || compareStringsIgnoreCase(l.last, c.last)) && ( | ||
// if this candidate has a party, find its lookup counterpart | ||
!c.party || compareStringsIgnoreCase(l.party, standardize.expandParty(c.party))) | ||
); | ||
}); | ||
var isMainAndRunning = !!mainCandidate && !mainCandidate.suspendedDate; | ||
return babelHelpers.extends({}, c, { | ||
isMainAndRunning: isMainAndRunning | ||
}); | ||
}); | ||
}, | ||
/** | ||
* Map candidates to color classes. | ||
@@ -902,6 +948,6 @@ * Useful for coloring choropleths. | ||
*/ | ||
makeColorMappings: function makeColorMappings(_ref) { | ||
var candidates = _ref.candidates; | ||
var party = _ref.party; | ||
var mainCandidates = _ref.mainCandidates; | ||
makeColorMappings: function makeColorMappings(_ref2) { | ||
var candidates = _ref2.candidates; | ||
var party = _ref2.party; | ||
var mainCandidates = _ref2.mainCandidates; | ||
@@ -908,0 +954,0 @@ |
65
doc.md
@@ -1,4 +0,4 @@ | ||
# Candidate | ||
# Candidates | ||
Various Candidate helper functions. | ||
Various Candidates helper functions. | ||
@@ -8,13 +8,15 @@ **Examples** | ||
```javascript | ||
import { Candidate } from 'election-utils' | ||
import { Candidates } from 'election-utils' | ||
``` | ||
## isWinner | ||
## addIsMainAndRunning | ||
Determine if this candidate is the winner. | ||
Will respect `winnerOverride` if present. | ||
Marge `candidates` and `lookupCandidates` by adding | ||
`isMainAndRunning` to each candidate. | ||
**Parameters** | ||
- `candidate` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the candidate | ||
- `$0.candidates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** an array of Candidates | ||
- `$1.lookupCandidates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** an array of lookup candidates | ||
- `_ref` | ||
@@ -24,17 +26,11 @@ **Examples** | ||
```javascript | ||
Candidate.isWinner(winningCandidate) //=> true | ||
import { Candidates, primaries2016Candidates } from 'election-utils' | ||
Candidates.addIsMainAndRunning({ | ||
candidates, | ||
lookupCandidates: primaries2016Candidates | ||
})[0] //=> { isMainAndRunning: true, ... } | ||
``` | ||
Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether candidate is winner | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** a new array of items augmented with `isMainAndRunning` | ||
# Candidates | ||
Various Candidates helper functions. | ||
**Examples** | ||
```javascript | ||
import { Candidates } from 'election-utils' | ||
``` | ||
## getVoteCount | ||
@@ -66,3 +62,3 @@ | ||
- `$2.mainCandidates` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** an array of main Candidates | ||
- `_ref` | ||
- `_ref2` | ||
@@ -94,2 +90,29 @@ **Examples** | ||
# Candidate | ||
Various Candidate helper functions. | ||
**Examples** | ||
```javascript | ||
import { Candidate } from 'election-utils' | ||
``` | ||
## isWinner | ||
Determine if this candidate is the winner. | ||
Will respect `winnerOverride` if present. | ||
**Parameters** | ||
- `candidate` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the candidate | ||
**Examples** | ||
```javascript | ||
Candidate.isWinner(winningCandidate) //=> true | ||
``` | ||
Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether candidate is winner | ||
# standardize | ||
@@ -203,3 +226,3 @@ | ||
import { formatTimestamp } from 'election-utils' | ||
formatTimestamp(results) //=> 'Mar 1 2:26 PM EST' | ||
formatTimestamp(results) //=> 'Mar. 1 2:26 PM EST' | ||
``` | ||
@@ -206,0 +229,0 @@ |
{ | ||
"name": "election-utils", | ||
"version": "3.3.2", | ||
"version": "3.4.0", | ||
"description": "This module provides various utility functions for dealing with AP election data.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
import orderBy from 'lodash.orderby'; | ||
import compare from './compareStringsIgnoreCase.js' | ||
import standardize from './standardize.js' | ||
@@ -12,2 +14,44 @@ /** | ||
/** | ||
* Marge `candidates` and `lookupCandidates` by adding | ||
* `isMainAndRunning` to each candidate. | ||
* | ||
* @param {Array} $0.candidates an array of Candidates | ||
* @param {Array} $1.lookupCandidates an array of lookup candidates | ||
* @returns {Array} a new array of items augmented with `isMainAndRunning` | ||
* @example | ||
* import { Candidates, primaries2016Candidates } from 'election-utils' | ||
* Candidates.addIsMainAndRunning({ | ||
* candidates, | ||
* lookupCandidates: primaries2016Candidates | ||
* })[0] //=> { isMainAndRunning: true, ... } | ||
*/ | ||
addIsMainAndRunning: ({ candidates, lookupCandidates }) => | ||
candidates.map(c => { | ||
// try to find this candidate in lookupCandidates | ||
const mainCandidate = lookupCandidates.find(l => | ||
// if this candidate has a first name, find its lookup counterpart | ||
(!c.first || compare(l.first, c.first)) && | ||
// if this candidate has a last name, find its lookup counterpart | ||
(!c.last || compare(l.last, c.last)) && | ||
// if this candidate has a party, find its lookup counterpart | ||
(!c.party || compare(l.party, standardize.expandParty(c.party))) | ||
) | ||
const isMainAndRunning = !!mainCandidate && | ||
!mainCandidate.suspendedDate | ||
return { | ||
...c, | ||
isMainAndRunning | ||
} | ||
}), | ||
/** | ||
* Map candidates to color classes. | ||
@@ -14,0 +58,0 @@ * Useful for coloring choropleths. |
@@ -7,7 +7,7 @@ const test = require('tape') | ||
const file = fs.readFileSync('./test/fixtures/20160301-MA.json', 'utf8') | ||
const file = fs.readFileSync('./test/fixtures/in/20160301-MA.json', 'utf8') | ||
const json = JSON.parse(file) | ||
assert.equal(formatTimestamp(json), 'Mar. 1 2:27 PM EST') | ||
assert.equal(formatTimestamp(json), 'Mar. 1 3:46 PM EST') | ||
assert.end() | ||
}) |
require('./standardize.js') | ||
require('./Candidates.js') | ||
require('./formatTimestamp.js') |
98527
25
3552
2