Skip to content

Instantly share code, notes, and snippets.

@zrrrzzt
Created June 4, 2018 10:57
Show Gist options
  • Save zrrrzzt/0e1b782d215fce14be9dafb9b23db7ab to your computer and use it in GitHub Desktop.
Save zrrrzzt/0e1b782d215fce14be9dafb9b23db7ab to your computer and use it in GitHub Desktop.
const data = require('./data.json')
function returnHighestColor (list) {
let color = 'ffffff'
if (list.includes('990099')) {
color = '990099'
} else if (list.includes('ff0000')) {
color = 'ff0000'
} else if (list.includes('ff9900')) {
color = 'ff9900'
} else if (list.includes('6ee86e')) {
color = '6ee86e'
}
return color
}
function replaceLetters (data) {
data = data.replace(' ', '')
data = data.replace('/', '-')
data = data.replace('æ', 'oe')
data = data.replace('å', 'aa')
data = data.replace('ø', 'oe')
return data
}
function generateIdFromFields (...args) {
return args.map(data => data.toString().toLowerCase()).map(replaceLetters).join('-')
}
const byStation = data.reduce((prev, current) => {
const eoi = current.eio || generateIdFromFields(current.area, current.zone, current.municipality)
if (!prev.hasOwnProperty(eoi)) {
prev[eoi] = Object.assign(current, {data: []})
}
prev[eoi].data.push({
component: current.component,
fromTime: current.fromTime,
toTime: current.toTime,
value: current.value,
unit: current.unit,
color: current.color
})
return prev
}, {})
const byStationAggregatedColor = Object.values(byStation).map(station => Object.assign(station, {color: returnHighestColor(station.data.map(data => data.color))}))
const byMunicipality = Object.values(byStationAggregatedColor).reduce((prev, current) => {
const municipality = current.municipality !== 'N/A' ? current.municipality : generateIdFromFields(current.area, current.zone, current.municipality)
if (!prev.hasOwnProperty(municipality)) {
prev[municipality] = {
zone: current.zone,
municipality: current.municipality,
area: current.area,
stations: []
}
}
prev[municipality].stations.push(current)
return prev
}, {})
const byMunicipalityAggregatedColor = Object.values(byMunicipality).map(municipality => Object.assign(municipality, {color: returnHighestColor(municipality.stations.map(data => data.color))}))
console.log(JSON.stringify(Object.values(byMunicipalityAggregatedColor), null, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment