Skip to content

Instantly share code, notes, and snippets.

@boehs
Created July 5, 2023 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boehs/0fc54a59fff1d84043da341d837c62df to your computer and use it in GitHub Desktop.
Save boehs/0fc54a59fff1d84043da341d837c62df to your computer and use it in GitHub Desktop.
OWID helper
let global: {
[entity: string]: {
[year: number]: {
[col: string]: string
}
}
} = {}
async function pCSV(fName: string) {
const f: string[][] = (await Bun.file(fName + ".csv").text()).split("\n").map(l => l.split(','));
const headings = f.shift()!
f.forEach((l,i) => {
let entity: string, year: string, value: string
l.forEach((e,i) => {
if (headings[i] == "Entity") entity = e
if (headings[i] == "Year") year = e
if (headings[i] == fName) value = e
if (year == undefined || entity == undefined) return
if (global[entity!] == undefined) global[entity!] = {}
if (global[entity!][year!] == undefined) global[entity!][year!] = {}
global[entity!][year!][fName] = value!
})
})
}
let wanted = [/*list of column headers*/]
for (let f of wanted) {
await pCSV(f)
}
let headings = ["Entity","Year",...wanted]
let str = headings.join(',')
Object.entries(global).forEach(([country, v]) => {
Object.entries(v).forEach(([year, v]) => {
let workingStr: string[] = [];
workingStr.push(country)
workingStr.push(year)
wanted.forEach(k => {
workingStr.push(v[k])
})
str += "\n" + workingStr.join(',')
})
})
Bun.write("output.csv", str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment