mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
48 lines
1.4 KiB
8 years ago
|
const _ = require('lodash');
|
||
|
|
||
|
// matches 'ft', 'mt', 'saint', and 'sainte' on word boundary
|
||
|
const mountSaintFort = /\b([fm]t|sainte?)\b/g;
|
||
|
|
||
8 years ago
|
const transliterations = {
|
||
|
'mt': 'mount',
|
||
|
'ft': 'fort',
|
||
|
'saint': 'st',
|
||
8 years ago
|
'sainte': 'ste'
|
||
|
};
|
||
|
|
||
8 years ago
|
function transliterate(match) {
|
||
|
return _.get(transliterations, match);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// transliterate ft/mt/saint/sainte to fort/mount/st/ste, respectively
|
||
8 years ago
|
function sanitize(raw, clean) {
|
||
|
// error & warning messages
|
||
|
// this function doesn't add any error or warning messages
|
||
|
const messages = { errors: [], warnings: [] };
|
||
|
|
||
8 years ago
|
// only try to transliterate if there is a city in parsed_text
|
||
8 years ago
|
if (!_.isEmpty(_.get(clean, 'parsed_text.city'))) {
|
||
8 years ago
|
// eg input: Ft. Saint Louis
|
||
|
// after 1. ft saint louis
|
||
|
// after 2. fort st louis
|
||
|
// after 3. fort st louis
|
||
|
|
||
|
// 1. remove '.' that could abbreviate ft and mt (makes transliteration regex easier)
|
||
|
const periods_removed = _.toLower(clean.parsed_text.city).replace(/\b(mt|ft)\./g, '$1 ');
|
||
|
|
||
|
// 2. transliterate 'saint'->'st', etc
|
||
|
const transliterated = periods_removed.replace(mountSaintFort, transliterate);
|
||
|
|
||
8 years ago
|
// 3. reduce whitespace sequences that can occur when removing periods down to a single space
|
||
8 years ago
|
const whitespace_normalized = _.trimEnd(transliterated.replace(/\s+/, ' '));
|
||
|
|
||
|
clean.parsed_text.city = whitespace_normalized;
|
||
|
|
||
8 years ago
|
}
|
||
|
|
||
|
return messages;
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = sanitize;
|