diff --git a/__tests__/requests/insee.spec.js b/__tests__/requests/insee.spec.js index bc133d14f9f923e37c2b69a027442b902431686f..5eff0f2e4349790be1d9cd3d9ac84a064e5908f1 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 16523319924ba91f36f77069b5d6b82e2045bbda..932be3813f654c35fd1dedae6a2742f5148c6e9e 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() }