From 0720b03f666cd9540d7688b96b06c5e48f52a242 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sun, 20 Dec 2015 01:12:31 -0500 Subject: [PATCH] In Taginfo results sort keys w ':' below keys w/o ':' (closes #2376) --- js/id/services/taginfo.js | 9 ++++++++- test/spec/taginfo.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/js/id/services/taginfo.js b/js/id/services/taginfo.js index 750aeb767..ade77ada7 100644 --- a/js/id/services/taginfo.js +++ b/js/id/services/taginfo.js @@ -58,6 +58,13 @@ iD.taginfo = function() { }; } + // sort keys with ':' lower than keys without ':' + function sortKeys(a, b) { + return (a.key.indexOf(':') === -1 && b.key.indexOf(':') !== -1) ? -1 + : (a.key.indexOf(':') !== -1 && b.key.indexOf(':') === -1) ? 1 + : 0; + } + var debounced = _.debounce(d3.json, 100, true); function request(url, debounce, callback) { @@ -86,7 +93,7 @@ iD.taginfo = function() { page: 1 }, parameters)), debounce, function(err, d) { if (err) return callback(err); - callback(null, d.data.filter(popularKeys(parameters)).map(valKey)); + callback(null, d.data.filter(popularKeys(parameters)).sort(sortKeys).map(valKey)); }); }; diff --git a/test/spec/taginfo.js b/test/spec/taginfo.js index dca47cca4..7d91d2f5b 100644 --- a/test/spec/taginfo.js +++ b/test/spec/taginfo.js @@ -55,6 +55,20 @@ describe("iD.taginfo", function() { expect(callback).to.have.been.calledWith(null, [{"value":"amenity"}]); }); + + it("sorts keys with ':' below keys without ':'", function() { + var callback = sinon.spy(); + + taginfo.keys({query: "ref"}, callback); + + server.respondWith("GET", new RegExp("https://taginfo.openstreetmap.org/api/4/keys/all"), + [200, { "Content-Type": "application/json" }, + '{"data":[{"key":"ref:bag","count_all":9790586,"count_all_fraction":0.0028},\ + {"key":"ref","count_all":7933528,"count_all_fraction":0.0023}]}']); + server.respond(); + + expect(callback).to.have.been.calledWith(null, [{"value":"ref"},{"value":"ref:bag"}]); + }); }); describe("#values", function() {