From 9db70bea54cc67d8c3cc33a4216530f777049f9b Mon Sep 17 00:00:00 2001 From: build-token <build-token> Date: Tue, 3 Jan 2023 10:00:20 +0000 Subject: [PATCH] publish: fix: improve tolerance of insee code generated from commit fc263eaadd49ea0135c4c34adbf7adaedf9e0f26 --- index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6990a81..6fb8baa 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, } -- GitLab