diff --git a/helper/labelGenerator.js b/helper/labelGenerator.js index 79f3f3e2..847a2e99 100644 --- a/helper/labelGenerator.js +++ b/helper/labelGenerator.js @@ -5,17 +5,20 @@ var stringUtils = require('../helper/stringUtils'); var normalize = stringUtils.normalize; var removeSpaces = stringUtils.removeSpaces; +const areaLikes = ['venue', 'neighbourhood', 'localadmin']; + module.exports = function( record, req ){ var schemaId = _.get(record, ['parent', 'country_a', 0 ], 'FIN'); var schema = getSchema(schemaId); // in virtually all cases, this will be the `name` field var labelParts = getInitialLabel(record); - + // do not add neighbourhood to largish area-like items + const skip = record.category?.includes('populated area') ? 'neighbourhood' : null; // iterate the schema for (var field in schema) { var valueFunction = schema[field]; - var value = valueFunction(record, req); + var value = valueFunction(record, req, skip); if(value) { labelParts.push(value); } @@ -25,18 +28,21 @@ module.exports = function( record, req ){ labelParts = _.compact(labelParts); // third, dedupe and join with a comma and return - return dedupeNameAndFirstLabelElement(labelParts).join(', '); - + const candDropMinorAdmin = areaLikes.includes(record.layer); + return dedupeNameAndFirstLabelElement(labelParts, candDropMinorAdmin).join(', '); }; -function dedupeNameAndFirstLabelElement(labelParts) { +function dedupeNameAndFirstLabelElement(labelParts, dropMinorAdmin) { // 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 - // 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"]` - - if (labelParts[0].toLowerCase() === labelParts[1].toLowerCase()) { + // 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"]` + // also, do not add a minor admin to a name of coarser admin, e.g. helsinki, kaartinkaupunki, helsinki + if ( + labelParts[0].toLowerCase() === labelParts[1].toLowerCase() || + dropMinorAdmin && labelParts.length > 2 && labelParts[0].toLowerCase() === labelParts[2].toLowerCase() + ) { labelParts.splice(1, 1); } } diff --git a/helper/labelSchema.js b/helper/labelSchema.js index 53af28a3..ef8e794a 100644 --- a/helper/labelSchema.js +++ b/helper/labelSchema.js @@ -36,7 +36,7 @@ function baseVal(val) { // find the first field of record that has a non-empty value that's not already in labelParts function getFirstProperty(fields, matchType, targets) { - return function(record, req) { + return function(record, req, skip) { if (targets && targets.indexOf(record.layer)===-1) { return null; // not applicable to this kind of record @@ -50,6 +50,9 @@ function getFirstProperty(fields, matchType, targets) { var bestField; for (var i = 0; i < fields.length; i++) { var field = fields[i]; + if (field === skip) { + continue; + } var fieldValue; if (Array.isArray(field)) { // chain multi parts var parts = [];