From 2f73f3e7d4b38e5810f84d5e81922ec0117943cd Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Fri, 20 Jan 2023 10:49:37 +0100
Subject: [PATCH] fix: handle communes with "St"

---
 __tests__/requests/insee.spec.js | 39 ++++++++++++++++++++++++++++++++
 src/requests/insee.js            |  2 ++
 2 files changed, 41 insertions(+)

diff --git a/__tests__/requests/insee.spec.js b/__tests__/requests/insee.spec.js
index bc133d1..5eff0f2 100644
--- a/__tests__/requests/insee.spec.js
+++ b/__tests__/requests/insee.spec.js
@@ -42,6 +42,45 @@ describe('getInseeCode', () => {
       expect(await getInseeCode(38080, "L'isle d'abeau")).toEqual('38193')
     })
 
+    describe('should handle communes with "Saint" or "St"', () => {
+      it("should return insee code for: Saint Romain au Mont d'Or", async () => {
+        expect(await getInseeCode(69270, "Saint Romain au Mont d'Or")).toEqual(
+          '69233'
+        )
+      })
+
+      it("should return insee code for: St Romain au Mont d'Or", async () => {
+        expect(await getInseeCode(69270, "St Romain au Mont d'Or")).toEqual(
+          '69233'
+        )
+      })
+
+      it('should return insee code for: Saint Genis les Ollières', async () => {
+        expect(await getInseeCode(69290, 'Saint Genis les Ollières')).toEqual(
+          '69205'
+        )
+      })
+
+      it('should return insee code for: St Genis les Ollières', async () => {
+        expect(await getInseeCode(69290, 'St Genis les Ollières')).toEqual(
+          '69205'
+        )
+      })
+
+      it('should return insee code for: St Priest', async () => {
+        // 69800 has only a single commune
+        expect(await getInseeCode(69800, 'st priest')).toEqual('69290')
+      })
+
+      // No regression test with replacement of st
+      it('should return insee code for: Puget-Rostang', async () => {
+        expect(await getInseeCode('06260', 'Puget-Rostang')).toEqual('06098')
+      })
+      it('should return insee code for: St léger', async () => {
+        expect(await getInseeCode('06260', 'St léger')).toEqual('06124')
+      })
+    })
+
     describe("should return correct insee code for Couzon-au-Mont-d'Or", () => {
       it("should return insee code for: Couzon au mont d'or", async () => {
         expect(await getInseeCode(69270, "Couzon au mont d'or")).toEqual(
diff --git a/src/requests/insee.js b/src/requests/insee.js
index 1652331..932be38 100644
--- a/src/requests/insee.js
+++ b/src/requests/insee.js
@@ -26,6 +26,7 @@ async function getInseeCode(postalCode, city) {
       const filteredResponse = response.data.filter(
         town => sanitizeCity(town.nomCommune) === parsedCity
       )
+
       return filteredResponse[0].codeCommune
     }
   } catch (error) {
@@ -44,6 +45,7 @@ async function getInseeCode(postalCode, city) {
 function sanitizeCity(city) {
   return city
     .toLowerCase()
+    .replace(/st/g, 'saint')
     .replace(/[âêîôûäëïü-\sʼ'éèç]/g, match => REPLACE_CHARS[match])
     .trim()
 }
-- 
GitLab