Skip to content

Instantly share code, notes, and snippets.

@zrrrzzt
Last active September 12, 2017 04:22
Show Gist options
  • Save zrrrzzt/2b563d1513d09565c523ed044080cbf7 to your computer and use it in GitHub Desktop.
Save zrrrzzt/2b563d1513d09565c523ed044080cbf7 to your computer and use it in GitHub Desktop.
Get mandates for the Norwegian election
'use strict'
const axios = require('axios')
const api = 'https://valgresultat.no/api/2017/st'
const REDS = ['MDG', 'RØDT', 'A', 'SV', 'SP']
module.exports = () => {
return new Promise(async (resolve, reject) => {
try {
const response = await axios.get(api)
const partier = response.data.partier
const repacked = partier.map(parti => Object.assign({
kode: parti.id.partikode,
navn: parti.id.navn,
mandater: parti.mandater.resultat ? parti.mandater.resultat.antall : parti.mandater.prognose.antall
}))
const score = repacked.reduce((a, b) => {
if (REDS.includes(b.kode)) {
a.red += b.mandater
} else {
a.blue += b.mandater
}
return a
}, {red: 0, blue: 0})
console.log(score)
resolve(score)
} catch (error) {
reject(error)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment