Incorporated some corrections from comments.

This commit is contained in:
Konstantin Mamalakis 2018-02-22 14:01:16 +02:00 committed by Josh Harvey
parent a3f8db79b3
commit 402f75f50c
3 changed files with 39 additions and 31 deletions

View file

@ -4,6 +4,7 @@ const util = require('util')
const XmlStream = require('xml-stream')
const nameUtils = require('./name-utils')
const options = require('../options')
const logger = require('../logger')
const _ = require('lodash/fp')
const debug_log = require('./debug') // KOSTIS TODO: remove
@ -59,6 +60,13 @@ const processAlias = _.curry((groupTypes, aliasNode) => {
const getNamePart = processDocumentedNamePart(groupTypes)
const latinNameNode = _.find(isLatin, aliasNode.DocumentedName)
if (!latinNameNode) {
const id = aliasNode.$.FixedRef
const message = `Alias for Person with ID="${id}" has no latinized name`
logger.error(message)
return
}
const namePartNodes = latinNameNode.DocumentedNamePart
const nameParts = _.map(getNamePart, namePartNodes)
@ -86,6 +94,7 @@ function processFeature (featureNode) {
if (featureNode.$.FeatureTypeID !== BIRTH_DATE) return
const datePeriodNode = featureNode.FeatureVersion.DatePeriod
// Ignore the fact that both Start and end can be a range.
// By using Start.From and End.To we use the extremes of the date-period.
const period = {
@ -107,9 +116,14 @@ function processProfile (profileNode) {
const groupTypesEntries = _.map(processMasterNamePartGroup, identityNode.NamePartGroups.MasterNamePartGroup)
const groupTypes = new Map(groupTypesEntries)
const mapCompact = _.flow(_.compact, _.map)
const getNameParts = processAlias(groupTypes)
const aliases = _.compact(_.map(getNameParts, identityNode.Alias))
const birthDatePeriods = _.compact(_.map(processFeature, profileNode.Feature))
const aliases = mapCompact(getNameParts, identityNode.Alias)
if (_.isEmpty(aliases)) return
const birthDatePeriods = mapCompact(processFeature, profileNode.Feature)
const individual = {aliases, birthDatePeriods}
debug_log(individual)
@ -135,7 +149,7 @@ function promiseParseDocument (source) {
xml.collect('MasterNamePartGroup')
const individuals = []
const collectResult = result => individuals.push(result)
const collectResult = result => result && individuals.push(result)
xml.on('updateElement: Profile', _.flow(processProfile, collectResult))
xml.on('end', _.wrap(resolve, individuals))
@ -147,8 +161,8 @@ const readdir = util.promisify(fs.readdir)
const combineAndDedupe = _.flow(
_.flatten,
_.compact,
_.uniqBy(_.nth(0)),
_.map(_.nth(1))
_.uniqBy(_.first),
_.map(_.last)
)
function parseList () {
@ -160,11 +174,7 @@ function parseList () {
}
return readdir(OFAC_DATA_DIR)
.then(sources => {
const promises = _.map(promiseParseDocument, sources)
return Promise.all(promises)
})
.then(sources => Promise.all(_.map(promiseParseDocument, sources)))
.then(combineAndDedupe)
}