-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from pelias/language-specific-labels
Language specific labels
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters