|
|
|
@ -1,26 +1,30 @@
|
|
|
|
|
const _ = require('lodash'); |
|
|
|
|
|
|
|
|
|
function isAdminOnly(parsed_text) { |
|
|
|
|
return ['number', 'street', 'query', 'category'].every((prop) => { |
|
|
|
|
return _.isEmpty(parsed_text[prop]); |
|
|
|
|
}); |
|
|
|
|
const non_admin_fields = ['number', 'street', 'query', 'category']; |
|
|
|
|
|
|
|
|
|
function hasAnyNonAdminFields(parsed_text) { |
|
|
|
|
return !_.isEmpty( |
|
|
|
|
_.intersection( |
|
|
|
|
_.keys(parsed_text), |
|
|
|
|
non_admin_fields)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function sanitize( raw, clean ){ |
|
|
|
|
// error & warning messages
|
|
|
|
|
const messages = { errors: [], warnings: [] }; |
|
|
|
|
|
|
|
|
|
// bail early if analysis wasn't admin-only
|
|
|
|
|
if (!isAdminOnly(clean.parsed_text)) { |
|
|
|
|
// bail early if analysis isn't admin-only
|
|
|
|
|
if (_.isUndefined(clean.parsed_text) || hasAnyNonAdminFields(clean.parsed_text)) { |
|
|
|
|
return messages; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// the analysis is admin-only, so add errors or warnings if geonames was requested
|
|
|
|
|
if (_.isEqual(clean.sources, ['geonames'])) { |
|
|
|
|
// if requested sources is only geonames, return an error
|
|
|
|
|
messages.errors.push('input contains only administrative area data, ' + |
|
|
|
|
'no results will be returned when sources=geonames'); |
|
|
|
|
|
|
|
|
|
} else if (_.indexOf(clean.sources, 'geonames') !== -1) { |
|
|
|
|
} else if (_.includes(clean.sources, 'geonames')) { |
|
|
|
|
// if there are other sources besides geonames, return an warning
|
|
|
|
|
messages.warnings.push('input contains only administrative area data, ' + |
|
|
|
|
'geonames results will not be returned'); |
|
|
|
|