Skip to content

Instantly share code, notes, and snippets.

@TobyEalden
Created May 3, 2017 14:38
Show Gist options
  • Save TobyEalden/e46a69ac0c5383bd92af14050d7257e9 to your computer and use it in GitHub Desktop.
Save TobyEalden/e46a69ac0c5383bd92af14050d7257e9 to your computer and use it in GitHub Desktop.
function mapData(populationData, ratioData, serviceArray, areaServiceId, deststream) {
// re-format the ratioData from an array to a dictionary lookup object
// in order to do the multiplications with poplet data
const ratioObject = dicLookup(ratioData, serviceArray);
const writePromises = [];
_.forEach(populationData, (populationObj) => {
const key = `${populationObj.area_id}${populationObj.gender}${populationObj.age_band}`;
if (ratioObject[key]) {
const rObj = {
areaServiceId: areaServiceId,
areaId: populationObj.area_id,
gender: populationObj.gender,
year: populationObj.year,
ageBand: populationObj.age_band,
};
console.log(`ratioObject has ${Object.keys(ratioObject[key]).length} keys`);
_.forEach(ratioObject[key], (ratio, serviceId) => {
// add serviceId field to each original poplet object
rObj.serviceId = serviceId;
// multiply with service ratio and persons each areaId, each demographic
rObj.count = ratio * populationObj.persons;
writePromises.push(new Promise((resolve, reject) => {
if (!deststream.write(`${JSON.stringify(rObj)}\n`)) {
// Wait for drain event
deststream.once("drain", resolve);
} else {
resolve();
}
}));
});
}
});
return Promise.all(writePromises);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment