From 84c788f173791b112a00c56136db2b84f8914824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Desboeufs?= Date: Tue, 22 Feb 2022 09:15:23 +0100 Subject: [PATCH] Update @etalab/decoupage-administratif (2.0.0) --- build/communes.js | 56 +++++++++++------------------------------------ package.json | 2 +- yarn.lock | 10 ++++----- 3 files changed, 18 insertions(+), 50 deletions(-) diff --git a/build/communes.js b/build/communes.js index f53973c..021a9d6 100644 --- a/build/communes.js +++ b/build/communes.js @@ -1,56 +1,26 @@ -const {groupBy, keyBy, maxBy} = require('lodash') -const historiqueCommunes = require('@etalab/decoupage-administratif/data/historique-communes.json') -const arrondissementsMunicipaux = require('@etalab/decoupage-administratif/data/communes.json') - .filter(c => c.type === 'arrondissement-municipal') - .map(c => ({code: c.code, nom: c.nom, type: 'COM'})) +const {keyBy} = require('lodash') -function connectGraph(historiqueCommunes) { - const byId = keyBy(historiqueCommunes, 'id') - for (const h of historiqueCommunes) { - if (h.successeur) { - h.successeur = byId[h.successeur] - } +const communes = require('@etalab/decoupage-administratif/data/communes.json') + .filter(c => ['commune-actuelle', 'arrondissement-municipal'].includes(c.type)) - if (h.predecesseur) { - h.predecesseur = byId[h.predecesseur] - } +const communesIndex = keyBy(communes, 'code') - if (h.pole) { - h.pole = byId[h.pole] - } +const anciensCodesIndex = new Map() - if (h.membres) { - h.membres = h.membres.map(m => byId[m]) +for (const commune of communes) { + if (commune.anciensCodes) { + for (const ancienCode of commune.anciensCodes) { + anciensCodesIndex.set(ancienCode, commune) } } } -connectGraph(historiqueCommunes) - -const byCodeCommune = groupBy(historiqueCommunes.concat(arrondissementsMunicipaux), h => `${h.type}${h.code}`) - -function getCommuneActuelle(communeEntry) { - if (typeof communeEntry === 'string') { - const candidates = byCodeCommune[`COM${communeEntry}`] - - if (candidates) { - return getCommuneActuelle(maxBy(candidates, c => c.dateFin || '9999-99-99')) - } - - return - } - - if (!communeEntry.dateFin && communeEntry.type === 'COM') { - return communeEntry +function getCommuneActuelle(codeCommune) { + if (codeCommune in communesIndex) { + return communesIndex[codeCommune] } - if (!communeEntry.dateFin) { - return getCommuneActuelle(communeEntry.pole) - } - - if (communeEntry.successeur) { - return getCommuneActuelle(communeEntry.successeur) - } + return anciensCodesIndex.get(codeCommune) } function expandWithCommune(codePostal) { diff --git a/package.json b/package.json index 7931308..6437ff6 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lodash": "^4.17.21" }, "devDependencies": { - "@etalab/decoupage-administratif": "^0.8.0", + "@etalab/decoupage-administratif": "^2.0.0", "ava": "^4.0.1", "csv-parser": "^3.0.0", "decompress": "^4.2.1", diff --git a/yarn.lock b/yarn.lock index 2236cbd..cb7a267 100644 --- a/yarn.lock +++ b/yarn.lock @@ -38,12 +38,10 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@etalab/decoupage-administratif@^0.8.0": - version "0.8.0" - resolved "https://registry.yarnpkg.com/@etalab/decoupage-administratif/-/decoupage-administratif-0.8.0.tgz#378a73a00737de5432179394dd28244a93bcbd46" - integrity sha512-BNO0BLfPyUvLpGAStdKpZAX80hUHdkhhD3GMu7HcwERqmshl96uIU0v3/1LlL23DiMs9qRyVIRnfDDIAfHaG/w== - dependencies: - lodash "^4.17.15" +"@etalab/decoupage-administratif@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@etalab/decoupage-administratif/-/decoupage-administratif-2.0.0.tgz#24ee640070c47e3c62883873c35450197fa5f78c" + integrity sha512-X+pQDnm0Sk8T5KPhT5kU9cLyMKc+LagK8ObYxyzVDoQapjLNZl9/KY2VFr42aw4LEiTnUfqCgNZc9lhHfi6shQ== "@humanwhocodes/config-array@^0.9.2": version "0.9.3"