Skip to content

Commit

Permalink
Update @etalab/decoupage-administratif (2.0.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Feb 22, 2022
1 parent c181d73 commit 84c788f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 50 deletions.
56 changes: 13 additions & 43 deletions build/communes.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 84c788f

Please sign in to comment.