diff --git a/modules/presets/preset.js b/modules/presets/preset.js index 581598bd6..3e5bfc45d 100644 --- a/modules/presets/preset.js +++ b/modules/presets/preset.js @@ -110,21 +110,34 @@ export function presetPreset(id, preset, fields, visible, rawPresets) { preset.matchScore = function(entityTags) { var tags = preset.tags; + var seen = {}; var score = 0; + var k; - for (var t in tags) { - if (entityTags[t] === tags[t]) { + // match on tags + for (k in tags) { + seen[k] = true; + if (entityTags[k] === tags[k]) { score += preset.originalScore; - } else if (tags[t] === '*' && t in entityTags) { + } else if (tags[k] === '*' && k in entityTags) { score += preset.originalScore / 2; } else { return -1; } } + // boost score for additional matches in addTags - #6802 + var addTags = preset.addTags; + for (k in addTags) { + if (!seen[k] && entityTags[k] === addTags[k]) { + score += preset.originalScore; + } + } + return score; }; + var _textCache = {}; preset.t = function(scope, options) { diff --git a/test/spec/presets/preset.js b/test/spec/presets/preset.js index 1ef42ba30..c09bd6bf6 100644 --- a/test/spec/presets/preset.js +++ b/test/spec/presets/preset.js @@ -44,6 +44,34 @@ describe('iD.presetPreset', function() { var entity = iD.osmWay({tags: {building: 'yep'}}); expect(preset.matchScore(entity.tags)).to.equal(0.5); }); + + it('boosts matchScore for additional matches in addTags', function() { + var presetSupercenter = iD.presetPreset('shop/supermarket/walmart_supercenter', { + tags: { 'brand:wikidata': 'Q483551', 'shop': 'supermarket' }, + addTags: { 'name': 'Walmart Supercenter' } + }); + var presetMarket = iD.presetPreset('shop/supermarket/walmart_market', { + tags: { 'brand:wikidata': 'Q483551', 'shop': 'supermarket' }, + addTags: { 'name': 'Walmart Neighborhood Market' } + }); + + var supercenter = iD.osmWay({ tags: { + 'brand:wikidata': 'Q483551', + 'shop': 'supermarket', + 'name': 'Walmart Supercenter' + }}); + var market = iD.osmWay({ tags: { + 'brand:wikidata': 'Q483551', + 'shop': 'supermarket', + 'name': 'Walmart Neighborhood Market' + }}); + + expect(presetSupercenter.matchScore(supercenter.tags)) + .to.be.greaterThan(presetMarket.matchScore(supercenter.tags)); + + expect(presetMarket.matchScore(market.tags)) + .to.be.greaterThan(presetSupercenter.matchScore(market.tags)); + }); }); describe('isFallback', function() {