Skip to content

Commit

Permalink
lint: add eslint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Apr 3, 2024
1 parent bf37d12 commit 1963ada
Show file tree
Hide file tree
Showing 31 changed files with 2,267 additions and 3,518 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"node": true
},
"extends": ["eslint:recommended", "prettier"],
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {}
}
60 changes: 41 additions & 19 deletions lib/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
const test = require('ava')
const {parseLocalizedField, parseErrorCode, getLabel, getErrorLevel} = require('../helpers')
const test = require("ava");
const {
parseLocalizedField,
parseErrorCode,
getLabel,
getErrorLevel,
} = require("../helpers");

test('parseLocalizedField', t => {
t.is(parseLocalizedField('voie_nom'), undefined)
t.deepEqual(parseLocalizedField('voie_nom_bre'), {schemaName: 'voie_nom', locale: 'bre'})
})
test("parseLocalizedField", (t) => {
t.is(parseLocalizedField("voie_nom"), undefined);
t.deepEqual(parseLocalizedField("voie_nom_bre"), {
schemaName: "voie_nom",
locale: "bre",
});
});

test('parseErrorCode', t => {
t.deepEqual(parseErrorCode('voie_nom.trop_long'), {schemaName: 'voie_nom', fieldError: 'trop_long'})
t.deepEqual(parseErrorCode('voie_nom_bre.trop_long'), {schemaName: 'voie_nom', locale: 'bre', fieldName: 'voie_nom_bre', fieldError: 'trop_long'})
})
test("parseErrorCode", (t) => {
t.deepEqual(parseErrorCode("voie_nom.trop_long"), {
schemaName: "voie_nom",
fieldError: "trop_long",
});
t.deepEqual(parseErrorCode("voie_nom_bre.trop_long"), {
schemaName: "voie_nom",
locale: "bre",
fieldName: "voie_nom_bre",
fieldError: "trop_long",
});
});

test('getLabel', t => {
t.is(getLabel('voie_nom.trop_court'), 'Le nom de la voie est trop court (3 caractères minimum)')
t.is(getLabel('voie_nom_bre.trop_court'), 'Le nom de la voie est trop court (3 caractères minimum) [bre]')
t.throws(() => getLabel('voie_nom_toto.trop_court'))
})
test("getLabel", (t) => {
t.is(
getLabel("voie_nom.trop_court"),
"Le nom de la voie est trop court (3 caractères minimum)",
);
t.is(
getLabel("voie_nom_bre.trop_court"),
"Le nom de la voie est trop court (3 caractères minimum) [bre]",
);
t.throws(() => getLabel("voie_nom_toto.trop_court"));
});

test('getErrorLevel', t => {
t.is(getErrorLevel('1.3-relax', 'voie_nom.trop_court'), 'E')
t.is(getErrorLevel('1.3-relax', 'voie_nom_bre.trop_court'), 'W')
})
test("getErrorLevel", (t) => {
t.is(getErrorLevel("1.3-relax", "voie_nom.trop_court"), "E");
t.is(getErrorLevel("1.3-relax", "voie_nom_bre.trop_court"), "W");
});
34 changes: 22 additions & 12 deletions lib/cog.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
const communes = require('../minicog.json')
const communes = require("../minicog.json");

const codesCommunesActuelles = new Set(communes.filter(c => !c.chefLieu).map(c => c.code))
const codesCommunesDeleguees = new Set(communes.filter(c => c.chefLieu).map(c => c.code))
const codesCommunesActuelles = new Set(
communes.filter((c) => !c.chefLieu).map((c) => c.code),
);
const codesCommunesDeleguees = new Set(
communes.filter((c) => c.chefLieu).map((c) => c.code),
);

const anciensCodesIndex = new Map()
const anciensCodesIndex = new Map();
for (const commune of communes) {
const anciensCodes = commune.anciensCodes || []
const anciensCodes = commune.anciensCodes || [];
for (const ancienCode of anciensCodes) {
anciensCodesIndex.set(ancienCode, commune)
anciensCodesIndex.set(ancienCode, commune);
}
}

function isCommune(codeCommune) {
return isCommuneActuelle(codeCommune) || isCommuneAncienne(codeCommune)
return isCommuneActuelle(codeCommune) || isCommuneAncienne(codeCommune);
}

function isCommuneAncienne(codeCommune) {
return anciensCodesIndex.has(codeCommune)
return anciensCodesIndex.has(codeCommune);
}

function isCommuneActuelle(codeCommune) {
return codesCommunesActuelles.has(codeCommune)
return codesCommunesActuelles.has(codeCommune);
}

function isCommuneDeleguee(codeCommune) {
return codesCommunesDeleguees.has(codeCommune)
return codesCommunesDeleguees.has(codeCommune);
}

function getCommuneActuelle(codeCommune) {
return anciensCodesIndex.has(codeCommune)
? anciensCodesIndex.get(codeCommune)
: communes.find(c => c.code === codeCommune && !c.chefLieu)
: communes.find((c) => c.code === codeCommune && !c.chefLieu);
}

module.exports = {isCommune, isCommuneAncienne, isCommuneActuelle, isCommuneDeleguee, getCommuneActuelle}
module.exports = {
isCommune,
isCommuneAncienne,
isCommuneActuelle,
isCommuneDeleguee,
getCommuneActuelle,
};
Loading

0 comments on commit 1963ada

Please sign in to comment.