Browse Source

added sanitizer to convert country iso2->iso3

pull/712/head
Stephen Hess 8 years ago
parent
commit
b18cb234b2
  1. 19
      sanitizer/_iso2_to_iso3.js
  2. 1
      sanitizer/component_geocoding.js
  3. 1
      test/unit/run.js
  4. 112
      test/unit/sanitizer/_iso2_to_iso3.js
  5. 5
      test/unit/sanitizer/component_geocoding.js

19
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;

1
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),

1
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'),

112
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);
}
};

5
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',

Loading…
Cancel
Save