Browse Source

Merge pull request #517 from pelias/fix-name-only-result-labels

fixed extraneous comma labels for results with only a `name`
pull/520/head
Stephen K Hess 9 years ago
parent
commit
daec7e9ebb
  1. 19
      helper/labelGenerator.js
  2. 11
      test/unit/helper/labelGenerator_examples.js

19
helper/labelGenerator.js

@ -16,18 +16,27 @@ module.exports = function( record ){
// retain only things that are truthy // retain only things that are truthy
labelParts = _.compact(labelParts); labelParts = _.compact(labelParts);
// third, dedupe and join with a comma and return
return dedupeNameAndFirstLabelElement(labelParts).join(', ');
};
function dedupeNameAndFirstLabelElement(labelParts) {
// only dedupe if a result has more than a name (the first label part)
if (labelParts.length > 1) {
// first, dedupe the name and 1st label array elements // first, dedupe the name and 1st label array elements
// this is used to ensure that the `name` and first admin hierarchy elements aren't repeated // this is used to ensure that the `name` and first admin hierarchy elements aren't repeated
// eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]` // eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]`
var dedupedNameAndFirstLabelElement = _.uniq([labelParts.shift(), labelParts.shift()]); var deduped = _.uniq([labelParts.shift(), labelParts.shift()]);
// second, unshift the deduped parts back onto the labelParts // second, unshift the deduped parts back onto the labelParts
labelParts.unshift.apply(labelParts, dedupedNameAndFirstLabelElement); labelParts.unshift.apply(labelParts, deduped);
// third, join with a comma and return }
return labelParts.join(', ');
}; return labelParts;
}
function getSchema(country_a) { function getSchema(country_a) {
if (country_a && country_a.length && schemas[country_a]) { if (country_a && country_a.length && schemas[country_a]) {

11
test/unit/helper/labelGenerator_examples.js

@ -104,6 +104,17 @@ module.exports.tests.france = function(test, common) {
}; };
module.exports.tests.name_only = function(test, common) {
test('name-only results (no admin fields) should not include extraneous comma', function(t) {
var doc = {
'name': 'Result name',
};
t.equal(generator(doc),'Result name');
t.end();
});
};
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {

Loading…
Cancel
Save