Created
May 3, 2017 14:38
-
-
Save TobyEalden/e46a69ac0c5383bd92af14050d7257e9 to your computer and use it in GitHub Desktop.
This file contains 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
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