Skip to content

Commit

Permalink
Merge pull request #51 from pelias/language-specific-labels
Browse files Browse the repository at this point in the history
Language specific labels
  • Loading branch information
orangejulius authored Mar 22, 2022
2 parents 971ca96 + d5b324f commit b943ac3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 13 deletions.
18 changes: 18 additions & 0 deletions getSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const _ = require('lodash');

const schemas = require('./labelSchema');

module.exports = function getSchema(record, language) {
if (_.isEmpty(record)) { return schemas.default; }
if (_.isEmpty(record.country_a)) { return schemas.default; }

// handle either array or scalar country codes, normalizing to upper case
const country_code_value = _.isArray(record.country_a) ? record.country_a[0] : record.country_a;
const country_code = _.isString(country_code_value) ? country_code_value.toUpperCase() : undefined;

const language_code = _.isString(language) ? language.toUpperCase() : undefined;

const schema = _.get(schemas[country_code], `languages.${language_code}`) || schemas[country_code] || schemas.default;

return schema;
};
21 changes: 8 additions & 13 deletions labelGenerator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const schemas = require('./labelSchema');

const getSchema = require('./getSchema');

function dedupeNameAndFirstLabelElement(labelParts) {
// only dedupe if a result has more than a name (the first label part)
Expand All @@ -16,16 +15,12 @@ function dedupeNameAndFirstLabelElement(labelParts) {
}

return labelParts;

}

function getSchema(country_a) {
if (!_.isEmpty(schemas[country_a])) {
return schemas[country_a[0]];
}

return schemas.default;
function getLanguage(language) {
if (!_.isString(language)) { return; }

return language.toUpperCase();
}

// this can go away once geonames is no longer supported
Expand Down Expand Up @@ -105,12 +100,12 @@ function defaultBuilder(schema, record) {
return dedupeNameAndFirstLabelElement(labelParts);
}

module.exports = function( record ){
const schema = getSchema(record.country_a);
module.exports = function( record, language ){
const schema = getSchema(record, language);
const separator = _.get(schema, ['meta','separator'], ', ');
const builder = _.get(schema, ['meta', 'builder'], defaultBuilder);

let labelParts = builder(schema, record);

return labelParts.join(separator);
return _.trim(labelParts.join(separator));
};
82 changes: 82 additions & 0 deletions test/getSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const getSchema = require('../getSchema');
const schemas = require('../labelSchema');

module.exports.tests = {};

module.exports.tests.schema_and_language_selection = function(test, common) {
test('no language and undefined record selects default schema', function(t) {
const actual = getSchema(undefined, undefined);
const expected = schemas['default'];

t.equals(actual, expected, 'default schema selected');
t.end();
});

test('no language and record with no country_a selects default schema', function(t) {
const actual = getSchema({}, undefined);
const expected = schemas['default'];

t.equals(actual, expected, 'default schema selected');
t.end();
});


test('no language and record with null country_a selects default schema', function(t) {
const actual = getSchema({ country_a: [ null ] }, undefined);
const expected = schemas['default'];

t.equals(actual, expected, 'default schema selected');
t.end();
});

test('no language and record with scalar country_a selects correct schema', function(t) {
const actual = getSchema({ country_a: 'usa' }, undefined);
const expected = schemas.USA;

t.equals(actual, expected, 'USA schema selected');
t.end();
});

test('invalid country_a selects default schema', function(t) {
const actual = getSchema({ country_a: ['XYZ']}, undefined);
const expected = schemas['default'];

t.equals(actual, expected, 'default schema selected');
t.end();
});

test('no language and record with array country_a selects correct schema', function(t) {
const actual = getSchema({ country_a: ['USA']}, undefined);
const expected = schemas.USA;

t.equals(actual, expected, 'USA schema selected');
t.end();
});

test('no language and record with lowercase country_a selects correct schema', function(t) {
const actual = getSchema({ country_a: ['usa']}, undefined);
const expected = schemas.USA;

t.equals(actual, expected, 'USA schema selected');
t.end();
});

test('valid but unspecified language and valid country_a selects correct schema', function(t) {
const actual = getSchema({ country_a: ['USA']}, 'ARA'); // no arabic custom format in USA
const expected = schemas.USA;

t.equals(actual, expected, 'USA schema selected');
t.end();
});
};

module.exports.all = function (tape, common) {

function test(name, testFunction) {
return tape('schemas: ' + name, testFunction);
}

for( const testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var tests = [
require ('./labelGenerator_KOR'),
require ('./labelGenerator_JPN'),
require ('./labelGenerator_FRA'),
require ('./getSchema'),
require ('./labelSchema')
];

Expand Down

0 comments on commit b943ac3

Please sign in to comment.