diff --git a/build/add-list.js b/build/add-list.js index ae763ae..65ee995 100644 --- a/build/add-list.js +++ b/build/add-list.js @@ -1,8 +1,8 @@ -const {join} = require('path') +const path = require('path') const {pick} = require('lodash') const readCsv = require('./read-csv') -const ADD_LIST_PATH = join(__dirname, '..', 'add-list.csv') +const ADD_LIST_PATH = path.join(__dirname, '..', 'add-list.csv') async function loadAddList() { const rows = await readCsv(ADD_LIST_PATH) diff --git a/build/communes.js b/build/communes.js index 956a6a6..f53973c 100644 --- a/build/communes.js +++ b/build/communes.js @@ -6,7 +6,7 @@ const arrondissementsMunicipaux = require('@etalab/decoupage-administratif/data/ function connectGraph(historiqueCommunes) { const byId = keyBy(historiqueCommunes, 'id') - historiqueCommunes.forEach(h => { + for (const h of historiqueCommunes) { if (h.successeur) { h.successeur = byId[h.successeur] } @@ -22,7 +22,7 @@ function connectGraph(historiqueCommunes) { if (h.membres) { h.membres = h.membres.map(m => byId[m]) } - }) + } } connectGraph(historiqueCommunes) diff --git a/build/extract-fimoct.js b/build/extract-fimoct.js index 04bd33a..9f7d4b8 100644 --- a/build/extract-fimoct.js +++ b/build/extract-fimoct.js @@ -21,17 +21,15 @@ function eachLine(line, ignoreList, cb) { const borneInferieureRepetition = line.substr(107, 1).trim() const indicateurAdressage = line.substr(115, 1).trim() - if (!codePostal.match(/(\d{5})/)) return cb() + if (!/(\d{5})/.test(codePostal)) return cb() if (codeCommune.startsWith('B')) return cb() // On ignore les codes postaux de pays étrangers if (codeCommune === '06900') return cb() // On ignore Monaco if (codeCommune === '97123') return cb() // On ignore Saint-Barthelemy if (codeCommune === '97127') return cb() // On ignore Saint-Martin // On ignore tous les couples codeCommune/codePostal renseignés dans le fichier CSV ignore-list.csv - if (codeCommune in ignoreList) { - if (ignoreList[codeCommune].some(item => item.codePostal === codePostal)) { - return cb() - } + if (codeCommune in ignoreList && ignoreList[codeCommune].some(item => item.codePostal === codePostal)) { + return cb() } const result = { diff --git a/build/ignore-list.js b/build/ignore-list.js index 242af19..45beb6d 100644 --- a/build/ignore-list.js +++ b/build/ignore-list.js @@ -1,8 +1,8 @@ -const {join} = require('path') +const path = require('path') const {groupBy} = require('lodash') const readCsv = require('./read-csv') -const IGNORE_LIST_PATH = join(__dirname, '..', 'ignore-list.csv') +const IGNORE_LIST_PATH = path.join(__dirname, '..', 'ignore-list.csv') async function loadIgnoreList() { const rows = await readCsv(IGNORE_LIST_PATH) diff --git a/build/index.js b/build/index.js index d594a04..f429dc0 100644 --- a/build/index.js +++ b/build/index.js @@ -1,6 +1,6 @@ -/* eslint unicorn/no-process-exit: off */ +#!/usr/bin/env node const fs = require('fs') -const {join} = require('path') +const path = require('path') const {promisify} = require('util') const {chain, pick} = require('lodash') const {getLatestFIMOCTFileBuffer} = require('./download-fimoct') @@ -57,10 +57,10 @@ function expandWithDefault(codesPostaux) { async function main() { const buffer = await getLatestFIMOCTFileBuffer() const codesPostaux = expandWithDefault(await extractFromFIMOCT(buffer)) - codesPostaux.forEach(item => expandWithCommune(item)) - await writeAsJSONFile(join(__dirname, '..', 'codes-postaux-full.json'), codesPostaux) + for (const item of codesPostaux) expandWithCommune(item) + await writeAsJSONFile(path.join(__dirname, '..', 'codes-postaux-full.json'), codesPostaux) const codesPostauxCompact = buildCompact(codesPostaux) - await writeAsJSONFile(join(__dirname, '..', 'codes-postaux.json'), codesPostauxCompact) + await writeAsJSONFile(path.join(__dirname, '..', 'codes-postaux.json'), codesPostauxCompact) } main().catch(error => { diff --git a/full.js b/full.js index 92c4a2c..457c6b8 100644 --- a/full.js +++ b/full.js @@ -70,11 +70,9 @@ function findCodePostal(codeCommune, idVoie, numero, repetition) { const entries = voiesIndex[idVoieMatch] if (entries.length === 1) return formatResult(entries[0]) const parsedNumero = Number.parseInt(numero, 10) - const candidate = entries.find(entry => { - return pariteOK(parsedNumero, entry.codeParite) && - superieurBorneInf(parsedNumero, repetition, entry.borneInferieure) && - inferieurBorneSup(parsedNumero, repetition, entry.borneSuperieure) - }) + const candidate = entries.find(entry => pariteOK(parsedNumero, entry.codeParite) + && superieurBorneInf(parsedNumero, repetition, entry.borneInferieure) + && inferieurBorneSup(parsedNumero, repetition, entry.borneSuperieure)) if (candidate) return formatResult(candidate) } diff --git a/index.js b/index.js index a6c340e..dbb7fde 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,12 @@ const index = {} -require('./codes-postaux.json').forEach(entry => { +for (const entry of require('./codes-postaux.json')) { if (!(entry.codePostal in index)) { index[entry.codePostal] = [] } index[entry.codePostal].push(entry) -}) +} exports.find = function (postalCode) { return index[postalCode] || [] diff --git a/package.json b/package.json index 1940c27..33a840a 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,8 @@ "comma-dangle": ["error", "never"], "unicorn/prefer-module": "off", "unicorn/prefer-string-slice": "off", - "import/extensions": "off" + "import/extensions": "off", + "node/prefer-global/process": "off" } }, diff --git a/test/basic.js b/test/basic.js index c9cbba9..b06ccb0 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,7 +2,7 @@ const test = require('ava') const codesPostaux = require('..') test('75002', t => { - const communes = codesPostaux.find(75002) + const communes = codesPostaux.find(75_002) t.is(communes.length, 1) const [commune] = communes t.is(commune.codePostal, '75002')