diff --git a/middleware/interpolate.js b/middleware/interpolate.js index f28e04db..b3fb5cdf 100644 --- a/middleware/interpolate.js +++ b/middleware/interpolate.js @@ -1,5 +1,6 @@ const async = require('async'); const logger = require( 'pelias-logger' ).get( 'api' ); +const source_mapping = require('../helper/type_mapping').source_mapping; const _ = require('lodash'); /** @@ -64,13 +65,13 @@ function setup(service, should_execute) { source_result.name.default = `${interpolation_result.properties.number} ${source_result.name.default}`; // -- source -- - if (interpolation_result.properties.source === 'OSM') { - source_result.source = 'openstreetmap'; - } else if (interpolation_result.properties.source === 'OA') { - source_result.source = 'openaddresses'; - } else { - source_result.source = 'mixed'; - } + // lookup the lowercased source, defaulting to 'mixed' when not found + // the source mapping is a jagged string->array, so default to 'mixed' as an array + // to ensure that subscript works + source_result.source = _.defaultTo( + source_mapping[_.toLower(interpolation_result.properties.source)], + ['mixed'] + )[0]; // -- source_id -- // note: interpolated values have no source_id @@ -109,7 +110,7 @@ function setup(service, should_execute) { }); }; - + } module.exports = setup; diff --git a/test/unit/middleware/interpolate.js b/test/unit/middleware/interpolate.js index 61da272f..de855854 100644 --- a/test/unit/middleware/interpolate.js +++ b/test/unit/middleware/interpolate.js @@ -225,8 +225,8 @@ module.exports.tests.success_conditions = (test, common) => { callback(null, { properties: { number: 17, - source: 'OSM', - source_id: 'openstreetmap source id', + source: 'Source Abbr 1', + source_id: 'source 1 source id', lat: 12.121212, lon: 21.212121 } @@ -236,8 +236,8 @@ module.exports.tests.success_conditions = (test, common) => { callback(null, { properties: { number: 18, - source: 'OA', - source_id: 'openaddresses source id', + source: 'Source Abbr 2', + source_id: 'source 2 source id', lat: 13.131313, lon: 31.313131 } @@ -264,7 +264,13 @@ module.exports.tests.success_conditions = (test, common) => { const logger = require('pelias-mock-logger')(); const controller = proxyquire('../../../middleware/interpolate', { - 'pelias-logger': logger + 'pelias-logger': logger, + '../helper/type_mapping': { + source_mapping: { + 'source abbr 1': ['full source name 1'], + 'source abbr 2': ['full source name 2'] + } + } })(service, () => true); const req = { @@ -331,8 +337,8 @@ module.exports.tests.success_conditions = (test, common) => { name: { default: '17 street name 1' }, - source: 'openstreetmap', - source_id: 'openstreetmap source id', + source: 'full source name 1', + source_id: 'source 1 source id', address_parts: { number: 17 }, @@ -348,8 +354,8 @@ module.exports.tests.success_conditions = (test, common) => { name: { default: '18 street name 3' }, - source: 'openaddresses', - source_id: 'openaddresses source id', + source: 'full source name 2', + source_id: 'source 2 source id', address_parts: { number: 18 },