From 5a61431e8b34cdde3c1acfbcbe2044b6791f9478 Mon Sep 17 00:00:00 2001 From: missinglink Date: Tue, 13 Aug 2019 17:28:18 +0200 Subject: [PATCH] feat(name): multi-stage fallback for when no default name was found --- stream/tag_mapper.js | 32 +++++++++++++++++++++---- test/stream/tag_mapper.js | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/stream/tag_mapper.js b/stream/tag_mapper.js index 8c0d5a10..ba505ba8 100644 --- a/stream/tag_mapper.js +++ b/stream/tag_mapper.js @@ -67,12 +67,34 @@ module.exports = function(){ } } - // Handle the case where no default name was set but there was - // an english name which we could use as the default. + // Handle the case where no default name was set but there were + // other names which we could use as the default. if( !doc.getName('default') ){ - var en = doc.getName('en'); - if( 'string' === typeof en ){ - doc.setName('default', en); + + var defaultName = + doc.getName('official') || + doc.getName('international') || + doc.getName('national') || + doc.getName('regional') || + doc.getName('en'); + + // use one of the preferred name tags listed above + if ( defaultName ){ + doc.setName('default', defaultName); + } + + // else try to use an available two-letter language name tag + else { + var keys = Object.keys(doc.name).filter(n => n.length === 2); + + // unambiguous (there is only a single two-letter name tag) + if ( keys.length === 1 ){ + doc.setName('default', doc.getName(keys[0])); + } + + // note we do not handle ambiguous cases where the record contains >1 + // two-letter name tags. + // see: https://github.com/pelias/openstreetmap/pull/498 } } diff --git a/test/stream/tag_mapper.js b/test/stream/tag_mapper.js index 2e0a55e3..4079331d 100644 --- a/test/stream/tag_mapper.js +++ b/test/stream/tag_mapper.js @@ -156,6 +156,25 @@ module.exports.tests.accept_localized_keys = function(test, common) { }); }; +// Use 'name:official' in situations where no other default name was available +// https://github.com/pelias/openstreetmap/issues/497 +module.exports.tests.use_official_as_fallback_default = function (test, common) { + var doc = new Document('a', 'b', 1); + doc.setMeta('tags', { 'name:ru': 'test1', 'name:en': 'test2', 'official_name': 'test3' }); + test('maps - use name:official as fallback default', function (t) { + var stream = mapper(); + stream.pipe(through.obj(function (doc, enc, next) { + t.equal(doc.getName('default'), 'test3', 'name:en used as fallback'); + t.equal(doc.getName('ru'), 'test1', 'correctly mapped'); + t.equal(doc.getName('en'), 'test2', 'correctly mapped'); + t.equal(doc.getName('official'), 'test3', 'correctly mapped'); + t.end(); // test will fail if not called (or called twice). + next(); + })); + stream.write(doc); + }); +}; + // Use 'name:en' in situations where no other default name was available // https://github.com/pelias/openstreetmap/issues/497 module.exports.tests.use_en_as_fallback_default = function (test, common) { @@ -175,6 +194,37 @@ module.exports.tests.use_en_as_fallback_default = function (test, common) { }); }; +// Use the *only* available name:** tag as fallback +module.exports.tests.unambiguous_default_name_fallback = function (test, common) { + var doc = new Document('a', 'b', 1); + doc.setMeta('tags', { 'name:ru': 'test1' }); + test('maps - unambiguous name:** used as default name fallback', function (t) { + var stream = mapper(); + stream.pipe(through.obj(function (doc, enc, next) { + t.equal(doc.getName('default'), 'test1', 'only name:** tag used as fallback'); + t.equal(doc.getName('ru'), 'test1', 'correctly mapped'); + t.end(); // test will fail if not called (or called twice). + next(); + })); + stream.write(doc); + }); +}; + +// In cases where there are multiple name:** tags, do not try to guess +module.exports.tests.ambiguous_default_name_fallback = function (test, common) { + var doc = new Document('a', 'b', 1); + doc.setMeta('tags', { 'name:ru': 'test1', 'name:pl': 'test2' }); + test('maps - ambiguous name:** not used as default name fallback', function (t) { + var stream = mapper(); + stream.pipe(through.obj(function (doc, enc, next) { + t.false(doc.getName('default'), 'do not try to guess'); + t.end(); // test will fail if not called (or called twice). + next(); + })); + stream.write(doc); + }); +}; + module.exports.tests.lowercase_keys = function(test, common) { var doc = new Document('a','b',1); doc.setMeta('tags', { 'name:EN': 'test' });