Skip to content

Commit

Permalink
feat(name): multi-stage fallback for when no default name was found
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Aug 14, 2019
1 parent 7db6842 commit 5a61431
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
32 changes: 27 additions & 5 deletions stream/tag_mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
50 changes: 50 additions & 0 deletions test/stream/tag_mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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' });
Expand Down

0 comments on commit 5a61431

Please sign in to comment.