Created
August 22, 2017 13:00
-
-
Save ticklemynausea/19a72f969c904c293c93bcdd17842f34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csv = require("csv"); | |
const process = require("process"); | |
const fs = require("fs"); | |
const _ = require("lodash"); | |
const files = { | |
source: "Explorer-NDC-Data.csv", | |
meta: "Explorer-Nav-And-Legend-Data.csv", | |
}; | |
const fromCSV = (content) => { | |
return new Promise((resolve, reject) => csv.parse(content, { | |
columns: (row) => row.map((r) => r.split("\n")[0]) | |
}, (error, data) => { | |
if (error) { | |
return reject(error); | |
} | |
return resolve(data); | |
})); | |
}; | |
const numberedTaggedTitleRxp = /(\d+\.\ )?(.+)(VIS|TXT)/; | |
const numberedTaggedTitle = (s) => { | |
let match = s.match(numberedTaggedTitleRxp); | |
return match && { | |
number: match[1] && match[1].split(".")[0], | |
title: match[2], | |
tag: match[3], | |
} | |
}; | |
const getBaseKey = (transformedSubCategory) => { | |
return transformedSubCategory.number ? | |
`${transformedSubCategory.number}. ${transformedSubCategory.title}` : | |
`${transformedSubCategory.title}` | |
}; | |
const collectIndicators = (d) => _(d).reduce((accumulator, value, key) => { | |
let transformedSubCategory = numberedTaggedTitle(key); | |
if (transformedSubCategory) { | |
let baseKey = getBaseKey(transformedSubCategory) | |
accumulator[transformedSubCategory.title] = { | |
vis: parseInt(d[`${baseKey}VIS`]), | |
txt: d[`${baseKey}TXT`], | |
}; | |
} | |
return accumulator; | |
}, {}); | |
const transformMetadata = (source) => (m) => { | |
const transformedSubCategory = numberedTaggedTitle(m.subCategory); | |
const baseKey = getBaseKey(transformedSubCategory); | |
return { | |
title: m.subCategoryNamingTXT, | |
mainCategory: m.belongsToMainCategory, | |
slug: transformedSubCategory.title, | |
legend: m.subCategoryLegendInfoTXT, | |
legendBuckets: _(m).reduce((accumulator, value, key) => { | |
let transformedSubCategory = numberedTaggedTitle(key); | |
if (value && transformedSubCategory && transformedSubCategory.title === "subCategoryLegendItem") { | |
return [...accumulator, { | |
vis: parseInt(transformedSubCategory.number)-1, | |
txt: value, | |
}]; | |
} | |
return accumulator; | |
}, []).sort((a,b) => a.vis - b.vis), | |
countries: _(source).map((d) => [ | |
d.countryCode, { | |
vis: parseInt(d[`${baseKey}VIS`]), | |
txt: d[`${baseKey}TXT`], | |
} | |
]).filter(d => d[1].vis).fromPairs().value(), | |
} | |
}; | |
Promise.all([ | |
fromCSV(fs.readFileSync(files.source)), | |
fromCSV(fs.readFileSync(files.meta)), | |
]) | |
.then(([source, meta]) => meta.map(transformMetadata(source))) | |
.then((result) => { | |
console.log(JSON.stringify(result, null, " ")); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment