From a8f12173acf0c04d0e982b9201e12568783074a7 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 31 Oct 2016 15:06:17 -0400 Subject: [PATCH] Multikeys filter should allow nested names, ignore alt prefixes --- modules/services/taginfo.js | 10 +++++++--- test/spec/services/taginfo.js | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/services/taginfo.js b/modules/services/taginfo.js index 0eb94d843..7683c5adc 100644 --- a/modules/services/taginfo.js +++ b/modules/services/taginfo.js @@ -69,9 +69,12 @@ function filterKeys(type) { } -function filterMultikeys() { +function filterMultikeys(prefix) { return function(d) { - return (d.key.match(/:/g) || []).length === 1; // exactly one ':' + // d.key begins with prefix, and d.key contains no additional ':'s + var re = new RegExp('^' + prefix + '(.*)$'); + var matches = d.key.match(re) || []; + return (matches.length === 2 && matches[1].indexOf(':') === -1); }; } @@ -177,6 +180,7 @@ export default { multikeys: function(parameters, callback) { var debounce = parameters.debounce; parameters = clean(setSort(parameters)); + var prefix = parameters.query; request(endpoint + 'keys/all?' + utilQsString(_.extend({ rp: 25, @@ -187,7 +191,7 @@ export default { if (err) { callback(err); } else { - var f = filterMultikeys(); + var f = filterMultikeys(prefix); callback(null, d.data.filter(f).map(valKey)); } } diff --git a/test/spec/services/taginfo.js b/test/spec/services/taginfo.js index 5e4630bd7..7ffff2840 100644 --- a/test/spec/services/taginfo.js +++ b/test/spec/services/taginfo.js @@ -102,15 +102,28 @@ describe('iD.serviceTaginfo', function() { it('excludes multikeys with extra colons', function() { var callback = sinon.spy(); - taginfo.multikeys({query: 'recycling:'}, callback); + taginfo.multikeys({query: 'service:bicycle:'}, callback); server.respondWith('GET', new RegExp('https://taginfo.openstreetmap.org/api/4/keys/all'), [200, { 'Content-Type': 'application/json' }, - '{"data":[{"count_all":69593,"key":"recycling:glass","count_all_fraction":0.0},' - + '{"count_all":22,"key":"recycling:glass:color","count_all_fraction":0.0}]}']); + '{"data":[{"count_all":4426,"key":"service:bicycle:retail","count_all_fraction":0.0},' + + '{"count_all":22,"key":"service:bicycle:retail:ebikes","count_all_fraction":0.0}]}']); server.respond(); - expect(callback).to.have.been.calledWith(null, [{'title':'recycling:glass', 'value':'recycling:glass'}]); + expect(callback).to.have.been.calledWith(null, [{'title':'service:bicycle:retail', 'value':'service:bicycle:retail'}]); + }); + + it('excludes multikeys with wrong prefix', function() { + var callback = sinon.spy(); + taginfo.multikeys({query: 'service:bicycle:'}, callback); + + server.respondWith('GET', new RegExp('https://taginfo.openstreetmap.org/api/4/keys/all'), + [200, { 'Content-Type': 'application/json' }, + '{"data":[{"count_all":4426,"key":"service:bicycle:retail","count_all_fraction":0.0},' + + '{"count_all":22,"key":"disused:service:bicycle","count_all_fraction":0.0}]}']); + server.respond(); + + expect(callback).to.have.been.calledWith(null, [{'title':'service:bicycle:retail', 'value':'service:bicycle:retail'}]); }); });