Skip to content
Snippets Groups Projects
Commit c87b49a9 authored by build-token's avatar build-token
Browse files

publish: Merge branch '9-ameliorer-la-tolerance-de-l-api-insee' into 'main'

generated from commit b5db05cb
parent de02db83
No related branches found
No related tags found
No related merge requests found
......@@ -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,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment