Comparing version 1.2.0 to 2.0.0
78
index.js
@@ -10,4 +10,4 @@ #! /usr/bin/env node | ||
function percRound(n) { | ||
return Math.round(n*10)/10; | ||
function perc(n) { | ||
return (Math.round(n*10)/10) + '%'; | ||
} | ||
@@ -23,10 +23,18 @@ | ||
function graphTextSplit(text, a, b) { | ||
const textLength = text.length; | ||
if (textLength <= a) { | ||
return [text + spaceTime(a - textLength), spaceTime(b)]; | ||
} else { | ||
const textRemainder = textLength - a; | ||
return [text.substr(0, a), text.substr(a) + spaceTime(b - textRemainder)]; | ||
function graphSplit(text, ns, sum) { | ||
const len = text.length; | ||
const widths = ns.map(n => Math.round((n / sum) * len)); | ||
var x = 0; | ||
for (let i = 0; i < widths.length; i++) { x += widths[i]; } | ||
widths[0] += len - x; | ||
var index = 0; | ||
var results = []; | ||
for (let i = 0; i < widths.length; i++) { | ||
const limit = index + widths[i]; | ||
results.push(text.substring(index, limit)); | ||
index = limit; | ||
} | ||
return results; | ||
} | ||
@@ -48,7 +56,19 @@ | ||
function printGraph(hillary, trump, model) { | ||
const modelLabel = rightpad(model.toUpperCase(), 8, ' '); | ||
const nowLabel = ' ' + modelLabel + ' Hillary: ' + percRound(hillary[model]) + '% – Trump: ' + percRound(trump[model]) + '%'; | ||
const nowLabelParts = graphTextSplit(nowLabel, Math.round(hillary[model]), Math.round(trump[model])); | ||
charm.foreground('white').background('blue').write(nowLabelParts[0]).background('red').write(nowLabelParts[1]); | ||
function printGraph(width, latest, model, metric) { | ||
const h = latest.D.models[model][metric]; | ||
const t = latest.R.models[model][metric]; | ||
const j = latest.L.models[model][metric]; | ||
const modelLabel = rightpad(model + ':' + metric, 14, ' '); | ||
const f = function (name, n) { return name + ': ' + perc(n); }; | ||
const prefix = ' ' + modelLabel + ' '; | ||
const sep = ' '; | ||
const label = prefix + f('Hillary', h) + sep + f('Trump', t) + sep + f('Johnson', j); | ||
const graph = rightpad(label, width, ' '); | ||
const graphParts = graphSplit(graph, [h, t, j], h + t + j); | ||
charm.foreground('white'); | ||
charm.background('blue').write(graphParts[0]); | ||
charm.background('red').write(graphParts[1]); | ||
charm.background('yellow').write(graphParts[2]); | ||
charm.display('reset'); | ||
@@ -58,27 +78,19 @@ } | ||
stream.on('end', function() { | ||
const width = process.stdout.columns; | ||
const data = JSON.parse(dataRegexp.exec(html)[0].replace('race.stateData = ', '')); | ||
const latest = data.forecasts.latest; | ||
const trump = { | ||
now: latest.R.models.now.winprob, | ||
polls: latest.R.models.polls.winprob, | ||
plus: latest.R.models.plus.winprob, | ||
}; | ||
const display = function (model, suffix) { | ||
printGraph(width, latest, model, 'forecast'); | ||
process.stdout.write('\n'); | ||
printGraph(width, latest, model, 'winprob'); | ||
process.stdout.write(suffix); | ||
} | ||
const hillary = { | ||
now: latest.D.models.now.winprob, | ||
polls: latest.D.models.polls.winprob, | ||
plus: latest.D.models.plus.winprob, | ||
}; | ||
printGraph(hillary, trump, 'now'); | ||
process.stdout.write('\n\n'); | ||
printGraph(hillary, trump, 'polls'); | ||
process.stdout.write('\n\n'); | ||
printGraph(hillary, trump, 'plus'); | ||
process.stdout.write('\n'); | ||
display('now', '\n\n'); | ||
display('polls', '\n\n'); | ||
display('plus', '\n'); | ||
charm.display('reset'); | ||
process.exit(); | ||
}); | ||
{ | ||
"name": "538", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Get the latest presidential polling forecasts from fivethirtyeight.com", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4621
74