diff --git a/labelGenerator.js b/labelGenerator.js index 84e9a46..cbbdac4 100644 --- a/labelGenerator.js +++ b/labelGenerator.js @@ -105,13 +105,16 @@ function buildPrefixLabelParts(schema, record) { } -function buildAdminLabelPart(schema, record) { +function buildAdminLabelPart(schema, record, { withOptional }) { let labelParts = []; + const optionals = _.get(schema, 'meta.optional', {}); // iterate the schema for (const field in schema.valueFunctions) { const valueFunction = schema.valueFunctions[field]; - labelParts.push(valueFunction(record)); + if (!optionals[field] || withOptional) { + labelParts.push(valueFunction(record)); + } } return labelParts; @@ -144,13 +147,14 @@ function buildPostfixLabelParts(schema, record) { return [record.name.default]; } -module.exports = function( record ){ +module.exports = function( record , options ){ const schema = getSchema(record.country_a); const separator = _.get(schema, ['meta','separator'], ', '); + const withOptional = _.get(options, 'withOptional', false); // in virtually all cases, this will be the `name` field const prefixParts = buildPrefixLabelParts(schema, record); - const adminParts = buildAdminLabelPart(schema, record); + const adminParts = buildAdminLabelPart(schema, record, { withOptional }); const postfixParts = buildPostfixLabelParts(schema, record); let labelParts = _.concat(prefixParts, adminParts, postfixParts); diff --git a/labelSchema.js b/labelSchema.js index 7e94a82..09f585c 100644 --- a/labelSchema.js +++ b/labelSchema.js @@ -90,6 +90,22 @@ function getFRACountryValue() { }; } + +// this function generates the region field for FRA records. +// 1. use nothing if the record is a in the French overseas or Paris (VP), +// eg - Saint-Denis, Reunion (instead of Saint-Denis, Reunion, Reunion) +// 2. use region name, eg - Bagneux, Hauts-De-Seine, France +// 3. use this with caution, Paris is both a locality and region. This can cause label like `Tour Eiffel, Paris, Paris, France` +function getFRARegionValue() { + const _default = getFirstProperty(['region']); + return (record) => { + if (!_.isEmpty(record.region_a) && (_.includes(FRA_OVERSEAS, record.region_a[0]) || record.region_a[0] === 'VP')) { + return undefined; + } + return _default(record); + }; +} + function isInNYC(record) { const _region_a = getFirstProperty(['region_a'])(record); const _country_a = getFirstProperty(['country_a'])(record); @@ -159,6 +175,11 @@ module.exports = { 'local': getFirstProperty(['locality', 'localadmin']), 'regional': getRegionalAbbreviation, 'country': getFirstProperty(['dependency', 'country']) + }, + 'meta': { + 'optional': { + 'regional': true + } } }, 'GBR': { @@ -203,7 +224,13 @@ module.exports = { 'FRA': { 'valueFunctions': { 'local': getFirstProperty(['locality', 'localadmin']), + 'region': getFRARegionValue(), 'country': getFRACountryValue() + }, + 'meta': { + 'optional': { + 'region': true + } } }, 'ITA': { diff --git a/test/labelGenerator_FRA.js b/test/labelGenerator_FRA.js index b825356..ff3efbb 100644 --- a/test/labelGenerator_FRA.js +++ b/test/labelGenerator_FRA.js @@ -27,6 +27,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'venue name, locality name, France'); + t.equal(generator(doc, {withOptional: true}),'venue name, locality name, region name, France'); t.end(); }); @@ -47,7 +48,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'venue name, locality name, Reunion'); + let expected = 'venue name, locality name, Reunion'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -67,6 +70,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'venue name, localadmin name, France'); + t.equal(generator(doc, {withOptional: true}),'venue name, localadmin name, region name, France'); t.end(); }); @@ -86,7 +90,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'venue name, localadmin name, Reunion'); + let expected = 'venue name, localadmin name, Reunion'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -107,6 +113,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'house number street name, locality name, France'); + t.equal(generator(doc, {withOptional: true}),'house number street name, locality name, region name, France'); t.end(); }); @@ -127,7 +134,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'house number street name, locality name, Martinique'); + const expected = 'house number street name, locality name, Martinique'; + t.equal(generator(doc), expected); + t.equal(generator(doc, {withOptional: true}), expected); t.end(); }); @@ -146,6 +155,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'neighbourhood name, locality name, France'); + t.equal(generator(doc, {withOptional: true}),'neighbourhood name, locality name, region name, France'); t.end(); }); @@ -164,7 +174,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'neighbourhood name, locality name, Martinique'); + let expected = 'neighbourhood name, locality name, Martinique'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -182,6 +194,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'locality name, France'); + t.equal(generator(doc, { withOptional: true }),'locality name, region name, France'); t.end(); }); @@ -199,7 +212,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'locality name, Guadeloupe'); + let expected = 'locality name, Guadeloupe'; + t.equal(generator(doc), expected); + t.equal(generator(doc, {withOptional: true}), expected); t.end(); }); @@ -216,6 +231,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'localadmin name, France'); + t.equal(generator(doc, { withOptional: true }),'localadmin name, region name, France'); t.end(); }); @@ -232,7 +248,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'localadmin name, French Guiana'); + let expected = 'localadmin name, French Guiana'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -248,6 +266,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'county name, France'); + t.equal(generator(doc, { withOptional: true }),'county name, region name, France'); t.end(); }); @@ -263,7 +282,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'county name, Reunion'); + let expected = 'county name, Reunion'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -278,6 +299,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'macrocounty name, France'); + t.equal(generator(doc, { withOptional: true }),'macrocounty name, region name, France'); t.end(); }); @@ -292,7 +314,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'macrocounty name, Reunion'); + let expected = 'macrocounty name, Reunion'; + t.equal(generator(doc), expected); + t.equal(generator(doc, { withOptional: true }), expected); t.end(); }); @@ -306,6 +330,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'region name, France'); + t.equal(generator(doc, { withOptional: true }),'region name, France'); t.end(); }); @@ -320,6 +345,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'Reunion'); + t.equal(generator(doc, { withOptional: true }),'Reunion'); t.end(); }); @@ -331,7 +357,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'macroregion name, France'); + let expected = 'macroregion name, France'; + t.equal(generator(doc),expected); + t.equal(generator(doc, { withOptional: true }),expected); t.end(); }); @@ -344,7 +372,9 @@ module.exports.tests.united_kingdom = function(test, common) { 'country_a': ['FRA'], 'country': ['France'] }; - t.equal(generator(doc),'dependency name'); + let expected = 'dependency name'; + t.equal(generator(doc),expected); + t.equal(generator(doc, { withOptional: true }),expected); t.end(); }); @@ -375,6 +405,7 @@ module.exports.tests.united_kingdom = function(test, common) { 'country': ['France'] }; t.equal(generator(doc),'locality name, dependency name'); + t.equal(generator(doc, { withOptional: true }),'locality name, region name, dependency name'); t.end(); }); diff --git a/test/labelGenerator_default.js b/test/labelGenerator_default.js index 8a47814..f511865 100644 --- a/test/labelGenerator_default.js +++ b/test/labelGenerator_default.js @@ -29,7 +29,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'venue name, locality name, region abbrv, country name'); + t.equal(generator(doc),'venue name, locality name, country name'); + t.equal(generator(doc, { withOptional: true }),'venue name, locality name, region abbrv, country name'); t.end(); }); @@ -49,7 +50,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'venue name, localadmin name, region abbrv, country name'); + t.equal(generator(doc),'venue name, localadmin name, country name'); + t.equal(generator(doc, { withOptional: true }),'venue name, localadmin name, region abbrv, country name'); t.end(); }); @@ -70,7 +72,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'house number street name, locality name, region abbrv, country name'); + t.equal(generator(doc),'house number street name, locality name, country name'); + t.equal(generator(doc, { withOptional: true }),'house number street name, locality name, region abbrv, country name'); t.end(); }); @@ -89,7 +92,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'neighbourhood name, locality name, region abbrv, country name'); + t.equal(generator(doc),'neighbourhood name, locality name, country name'); + t.equal(generator(doc, { withOptional: true }),'neighbourhood name, locality name, region abbrv, country name'); t.end(); }); @@ -107,7 +111,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'locality name, region abbrv, country name'); + t.equal(generator(doc),'locality name, country name'); + t.equal(generator(doc, { withOptional: true }),'locality name, region abbrv, country name'); t.end(); }); @@ -124,7 +129,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'localadmin name, region abbrv, country name'); + t.equal(generator(doc),'localadmin name, country name'); + t.equal(generator(doc, { withOptional: true }),'localadmin name, region abbrv, country name'); t.end(); }); @@ -140,7 +146,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'county name, region abbrv, country name'); + t.equal(generator(doc),'county name, country name'); + t.equal(generator(doc, { withOptional: true }),'county name, region abbrv, country name'); t.end(); }); @@ -155,7 +162,8 @@ module.exports.tests.default_country = function(test, common) { 'country_a': ['country code'], 'country': ['country name'] }; - t.equal(generator(doc),'macrocounty name, region abbrv, country name'); + t.equal(generator(doc),'macrocounty name, country name'); + t.equal(generator(doc, { withOptional: true }),'macrocounty name, region abbrv, country name'); t.end(); }); @@ -170,6 +178,7 @@ module.exports.tests.default_country = function(test, common) { 'country': ['country name'] }; t.equal(generator(doc),'region name, country name'); + t.equal(generator(doc, { withOptional: true }),'region name, country name'); t.end(); }); @@ -182,6 +191,7 @@ module.exports.tests.default_country = function(test, common) { 'country': ['country name'] }; t.equal(generator(doc),'macroregion name, country name'); + t.equal(generator(doc, { withOptional: true }),'macroregion name, country name'); t.end(); }); @@ -223,6 +233,7 @@ module.exports.tests.default_country = function(test, common) { 'country': ['country name'] }; t.equal(generator(doc),'locality name, dependency name'); + t.equal(generator(doc, { withOptional: true }),'locality name, dependency name'); t.end(); }); @@ -235,6 +246,7 @@ module.exports.tests.default_country = function(test, common) { //note no country_a }; t.equal(generator(doc),'region name'); + t.equal(generator(doc, { withOptional: true }),'region name'); t.end(); }); diff --git a/test/labelSchema.js b/test/labelSchema.js index 8b7cb2e..081f726 100644 --- a/test/labelSchema.js +++ b/test/labelSchema.js @@ -31,7 +31,7 @@ module.exports.tests.supported_countries = function(test, common) { t.equals(Object.keys(schemas.GBR.valueFunctions).length, 3); t.equals(Object.keys(schemas.AUS.valueFunctions).length, 3); t.equals(Object.keys(schemas.KOR.valueFunctions).length, 3); - t.equals(Object.keys(schemas.FRA.valueFunctions).length, 2); + t.equals(Object.keys(schemas.FRA.valueFunctions).length, 3); t.equals(Object.keys(schemas.ITA.valueFunctions).length, 3); t.equals(Object.keys(schemas.default.valueFunctions).length, 3);