From b18cb234b2f5c4b9f9bda9758ba967e80c80e9a4 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 11 Nov 2016 14:22:01 -0500 Subject: [PATCH] added sanitizer to convert country iso2->iso3 --- sanitizer/_iso2_to_iso3.js | 19 ++++ sanitizer/component_geocoding.js | 1 + test/unit/run.js | 1 + test/unit/sanitizer/_iso2_to_iso3.js | 112 +++++++++++++++++++++ test/unit/sanitizer/component_geocoding.js | 5 + 5 files changed, 138 insertions(+) create mode 100644 sanitizer/_iso2_to_iso3.js create mode 100644 test/unit/sanitizer/_iso2_to_iso3.js diff --git a/sanitizer/_iso2_to_iso3.js b/sanitizer/_iso2_to_iso3.js new file mode 100644 index 00000000..d8fc1837 --- /dev/null +++ b/sanitizer/_iso2_to_iso3.js @@ -0,0 +1,19 @@ +const _ = require('lodash'); +const iso3166 = require('iso3166-1'); + +// this sanitizer exists solely to convert an ISO2 country value to ISO3 +// eg - 'TH' -> 'THA' +// this can go away once altnames imports ISO2 country values from WOF +function sanitize( raw, clean ){ + // error & warning messages + const messages = { errors: [], warnings: [] }; + + if (clean.hasOwnProperty('parsed_text') && iso3166.is2(_.toUpper(clean.parsed_text.country))) { + clean.parsed_text.country = iso3166.to3(_.toUpper(clean.parsed_text.country)); + } + + return messages; +} + +// export function +module.exports = sanitize; diff --git a/sanitizer/component_geocoding.js b/sanitizer/component_geocoding.js index 0ab44fc1..a5a95de9 100644 --- a/sanitizer/component_geocoding.js +++ b/sanitizer/component_geocoding.js @@ -5,6 +5,7 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), quattroshapes_deprecation: require('../sanitizer/_deprecate_quattroshapes'), singleScalarParameters: require('../sanitizer/_single_scalar_parameters'), synthesize_analysis: require('../sanitizer/_synthesize_analysis'), + iso2_to_iso3: require('../sanitizer/_iso2_to_iso3'), size: require('../sanitizer/_size')(/* use defaults*/), layers: require('../sanitizer/_targets')('layers', type_mapping.layer_mapping), sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), diff --git a/test/unit/run.js b/test/unit/run.js index 49991d05..6ef7d417 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -46,6 +46,7 @@ var tests = [ require('./sanitizer/_geo_reverse'), require('./sanitizer/_groups'), require('./sanitizer/_ids'), + require('./sanitizer/_iso2_to_iso3'), require('./sanitizer/_layers'), require('./sanitizer/_single_scalar_parameters'), require('./sanitizer/_size'), diff --git a/test/unit/sanitizer/_iso2_to_iso3.js b/test/unit/sanitizer/_iso2_to_iso3.js new file mode 100644 index 00000000..7d27499b --- /dev/null +++ b/test/unit/sanitizer/_iso2_to_iso3.js @@ -0,0 +1,112 @@ +const sanitizer = require('../../../sanitizer/_iso2_to_iso3'); + +module.exports.tests = {}; + +module.exports.tests.text_parser = function(test, common) { + test('clean without parsed_text should not throw exception', function(t) { + const raw = {}; + + const clean = { + }; + + const expected_clean = { + }; + + const messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + + test('country with known iso2 should be converted to iso3', function(t) { + const raw = {}; + + const clean = { + parsed_text: { + address: 'address value', + country: 'tH' + } + }; + + const expected_clean = { + parsed_text: { + address: 'address value', + country: 'THA' + } + }; + + const messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + + test('country with unknown iso2 should be unchanged', function(t) { + const raw = {}; + + const clean = { + parsed_text: { + address: 'address value', + country: 'TB' + } + }; + + const expected_clean = { + parsed_text: { + address: 'address value', + country: 'TB' + } + }; + + const messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + + test('undefined country should be unchanged', function(t) { + const raw = {}; + + const clean = { + parsed_text: { + address: 'address value', + country: undefined + } + }; + + const expected_clean = { + parsed_text: { + address: 'address value', + country: undefined + } + }; + + const messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + +}; + +module.exports.all = function (tape, common) { + function test(name, testFunction) { + return tape('sanitizer _iso2_to_iso3: ' + name, testFunction); + } + + for( const testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/sanitizer/component_geocoding.js b/test/unit/sanitizer/component_geocoding.js index 079f92ba..6477b738 100644 --- a/test/unit/sanitizer/component_geocoding.js +++ b/test/unit/sanitizer/component_geocoding.js @@ -21,6 +21,10 @@ module.exports.tests.sanitize = function(test, common) { called_sanitizers.push('_synthesize_analysis'); return { errors: [], warnings: [] }; }, + '../sanitizer/_iso2_to_iso3': function() { + called_sanitizers.push('_iso2_to_iso3'); + return { errors: [], warnings: [] }; + }, '../sanitizer/_size': function() { if (arguments.length === 0) { return function() { @@ -81,6 +85,7 @@ module.exports.tests.sanitize = function(test, common) { '_deprecate_quattroshapes', '_single_scalar_parameters', '_synthesize_analysis', + '_iso2_to_iso3', '_size', '_targets/layers', '_targets/sources',