Skip to content

Instantly share code, notes, and snippets.

@mojowen
Last active October 3, 2016 18:11
Show Gist options
  • Save mojowen/4f1b805ed7201e47961ad491b193d838 to your computer and use it in GitHub Desktop.
Save mojowen/4f1b805ed7201e47961ad491b193d838 to your computer and use it in GitHub Desktop.
Can You Vote?
// Generates the JSON from http://felonvoting.procon.org/view.resource.php?resourceID=000286
var states = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
var data = {}
$('tr')
.filter(function() {
return (($(this).find('td:first').text()[0] || '').match(/\d/)
&& $(this).find('td:eq(1)').text().length > 0) })
.slice(51, 102)
.map( function() {
var inmates_vote = $(this).find('td:eq(11) img').length > 0,
parolees_vote = $(this).find('td:eq(7) img').length > 0 || inmates_vote,
probationaries_vote = $(this).find('td:eq(9) img').length > 0 || parolees_vote,
felons_vote = $(this).find('td:eq(3) img').length < 1 || probationaries_vote
return {
state: $(this).find('td:eq(1)').text(),
inmates_vote: inmates_vote,
parolees_vote: parolees_vote,
probationaries_vote: probationaries_vote,
felons_vote: felons_vote }})
.each( function(i, el) { data[ states[i] ] = el })
console.log( JSON.stringify(data) ) // JSON Format
flat = [["State Abreviation",
"State Name",
"Inmates Vote?",
"Parolees Vote?",
"Probationaries Vote?",
"Felons Vote?"].join("\t")]
for(var abv in data) {
var state = states[abv].state
flat.push([abv,
state.state,
states.inmates_vote ? 'Yes' : 'No',
states.parolees_vote ? 'Yes' : 'No',
states.probationaries_vote ? 'Yes' : 'No',
states.felons_vote ? 'Yes' : 'No'].join("\t"))
}
console.log( flat.join("\n") ) // Spreadsheet Format
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: sans-serif;
text-align: center;
padding: 50px 0;
}
h1 {
margin: 0 auto;
width: 80%;
}
select {
font-size: 24px;
}
button {
font-size: 18px;
padding: 8px 14px;
background: white;
margin: 0 10px;
}
</style>
<script src="questionnaire.js"></script>
<script>
function handleGoogle(result) {
window.rights_by_state = {}
for(var i = 0; i < result.feed.entry.length; i ++) {
var raw_state = result.feed.entry[i],
content = raw_state.content['$t'].split(', '),
state = { }
for( var j = 0; j < content.length; j++ ) {
var entry = content[j].split(': '),
key = entry[0],
value = entry = entry[1]
if( key === 'state' ) state.state = value
else state[key+'_vote'] = value === 'Yes'
}
var option = document.createElement('option')
option.value = raw_state.title['$t']
option.innerText = state.state
states.appendChild(option)
rights_by_state[raw_state.title['$t']] = state
}
states.onchange = function() {
if( this.value ) {
this.parentElement.style.display = 'none'
questionnaire.style.display = 'block'
new Questionnaire(questionnaire , rights_by_state[this.value])
}
}
}
</script>
</head>
<body>
<div>
<h1>Curious if you can Vote?</h1>
<br />
<p>Check whether you're eligibile by using this simple form. Data from <a href="http://felonvoting.procon.org/view.resource.php?resourceID=000286">ProCon.org</a></p>
<select id="states"><option>Select your State</option></select>
</div>
<form id="questionnaire" style="display:none">
<h1 id="question">Have you ever been convicted of a felony?</h1>
<br />
<button class="yes">Yes</button>
<button class="no">No</button>
</form>
<script src="https://spreadsheets.google.com/feeds/list/1L4AnQNuFQBf1WI5BAd4cuqOrD0M8-dCuaA4syutLDR0/od6/public/basic?alt=json-in-script&callback=handleGoogle"></script>
</body>
</html>
function Questionnaire(form, state) {
this.state = state
this.step = 0
this.clicked = null
this.flow = {
false: true,
true: {
true: 'probationaries_voteinmates_vote',
false: {
true: 'inmates_vote',
false: {
true: 'parolees_vote',
false: 'felons_vote'
}
}
}
}
this.steps = [{
step: "felons",
question: "Have you ever been convicted of a felony?",
}, {
step: "probationaries",
question: "Are you currently on probation?"
}, {
step: "inmates",
question: "Are you currently incarcerated?",
}, {
step: "parolees",
question: "Are you currently on parole?"
}]
this.stateConditions = function() {
var conditions = this.state_steps.filter(function(el) { return state[el] }).length
return this.state.state + [null,
' after completing your sentence including prison, parole, and probation.',
' after completing your sentence including prison and parole. If you are on probation you can vote.',
' after completing your sentence including prison. If you are on parole or probation you can vote.',
'. If you are in prison, on parole, or on probation you can vote.'][conditions]
}
this.state_steps = ['probationaries_vote', 'inmates_vote', 'parolees_vote', 'felons_vote']
this.can_vote = ["Based on your response, you sound eligible",
"to vote in",
this.stateConditions(),
"You can use the following voter",
"registration tool to get registered"
].join(' ')
this.cannot_vote = ["Based on your response, we believe that",
this.state.state,
"does not allow you to vote. Please sign",
"this petition encouraging the state to take",
"action to allow all citizens to participate"
].join(' ')
this.endStep = function(verdict, form) {
var verdict = verdict ? "can_vote" : "cannot_vote"
form.innerHTML = "<h1>" + this[verdict] + "</h1>"
}
this.wall = function(step) {
var state = this.state
return this.state_steps.slice(step, 4).filter(function(el) {
return state[el]
}).length < 1
}
this.window = function(step) {
var state = this.state
return this.state_steps.slice(step, 4).filter(function(el) {
return !state[el]
}).length < 1
}
this.nextStep = function(form) {
var state = this.flow[this.clicked]
if(typeof state === 'object') {
this.step += 1
if( this.wall(this.step) ) {
this.endStep(false, form)
} else if ( this.window(this.step) ) {
this.endStep(true, form)
} else {
this.flow = state
form.querySelector('h1').innerText = this.steps[this.step].question
}
} else {
if( typeof state === 'boolean' ) this.endStep(state, form)
else this.endStep(this.state[state], form)
}
return false
}
var that = this
form.querySelector('.yes').onclick = function() {
that.clicked = true
}
form.querySelector('.no').onclick = function() {
that.clicked = false
}
form.onsubmit = function() {
return that.nextStep(this)
}
return this
}
// Example of data after it's transformed from a request to google spreadsheets.
// Data stored in https://docs.google.com/spreadsheets/d/1L4AnQNuFQBf1WI5BAd4cuqOrD0M8-dCuaA4syutLDR0/pubhtml
// and transformed into the following format
{
"AL": {
"state": "Alabama",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"AK": {
"state": "Alaska",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"AZ": {
"state": "Arizona",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"AR": {
"state": "Arkansas",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"CA": {
"state": "California",
"inmates_vote": false,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"CO": {
"state": "Colorado",
"inmates_vote": false,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"CT": {
"state": "Connecticut",
"inmates_vote": false,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"DC": {
"state": "District of Columbia",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"DE": {
"state": "Delaware",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"FL": {
"state": "Florida",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"GA": {
"state": "Georgia",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"HI": {
"state": "Hawaii",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"ID": {
"state": "Idaho",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"IL": {
"state": "Illinois",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"IN": {
"state": "Indiana",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"IA": {
"state": "Iowa",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"KS": {
"state": "Kansas",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"KY": {
"state": "Kentucky",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"LA": {
"state": "Louisiana",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"ME": {
"state": "Maine",
"inmates_vote": true,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"MD": {
"state": "Maryland",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"MA": {
"state": "Massachusetts",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"MI": {
"state": "Michigan",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"MN": {
"state": "Minnesota",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"MS": {
"state": "Mississippi",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"MO": {
"state": "Missouri",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"MT": {
"state": "Montana",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"NE": {
"state": "Nebraska",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"NV": {
"state": "Nevada",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"NH": {
"state": "New Hampshire",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"NJ": {
"state": "New Jersey",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"NM": {
"state": "New Mexico",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"NY": {
"state": "New York",
"inmates_vote": false,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"NC": {
"state": "North Carolina",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"ND": {
"state": "North Dakota",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"OH": {
"state": "Ohio",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"OK": {
"state": "Oklahoma",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"OR": {
"state": "Oregon",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"PA": {
"state": "Pennsylvania",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"RI": {
"state": "Rhode Island",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"SC": {
"state": "South Carolina",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"SD": {
"state": "South Dakota",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"TN": {
"state": "Tennessee",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
},
"TX": {
"state": "Texas",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"UT": {
"state": "Utah",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": true,
"felons_vote": true
},
"VT": {
"state": "Vermont",
"inmates_vote": true,
"parolees_vote": true,
"probationaries_vote": true,
"felons_vote": true
},
"VA": {
"state": "Virginia",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"WA": {
"state": "Washington",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"WV": {
"state": "West Virginia",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"WI": {
"state": "Wisconsin",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": true
},
"WY": {
"state": "Wyoming",
"inmates_vote": false,
"parolees_vote": false,
"probationaries_vote": false,
"felons_vote": false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment