diff --git a/index.js b/index.js index 6990a81087a5a3f602b8c69e5286d223defeb274..6fb8baa21142724254530cd986eb3d969b2ad6d0 100644 --- a/index.js +++ b/index.js @@ -242120,8 +242120,10 @@ async function getInseeCode(postalCode, city) { } else { if (!city) throw new Error('No city') + const parsedCity = sanitizeCity(city) + const filteredResponse = response.data.filter( - town => town.nomCommune.toLowerCase() === city.toLowerCase() + town => sanitizeCity(town.nomCommune) === parsedCity ) return filteredResponse[0].codeCommune } @@ -242133,6 +242135,38 @@ async function getInseeCode(postalCode, city) { } } +/** + * Clean city input and remove all characters such as (^, ¨, ʼ, ', -, é, è) + * @param {string} city + * @return {string} parsedCity + */ +function sanitizeCity(city) { + return city + .toLowerCase() + .replace(/[âêîôûäëïü-\sʼ'éèç]/g, match => REPLACE_CHARS[match]) + .trim() +} + +const REPLACE_CHARS = { + â: 'a', + ê: 'e', + î: 'i', + ô: 'o', + û: 'u', + ä: 'a', + ë: 'e', + ï: 'i', + ö: 'o', + ü: 'u', + '-': '', + ' ': '', + ʼ: '', + "'": '', + é: 'e', + è: 'e', + ç: 'c', +} + module.exports = { getInseeCode, }