Skip to content

Commit

Permalink
fix(core): use suglify to find commune by name
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 25, 2024
1 parent 8b666eb commit 71fc88c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

// Export the compiled rules and the types.

import compiledRules from "../build";
export type { Questions, RuleName } from "../build";
import compiledRules from "../publicodes-build";
export type { Questions, RuleName } from "../publicodes-build";
/**
* Publicodes rules compiled in a single JSON object.
*/
Expand All @@ -20,6 +20,10 @@ export const rules = compiledRules;
export * as data from "./data";
export type { AideRuleNames, Commune, Localisation } from "./data";

// Export the utils functions.

export { slugify } from "./lib/utils";

// Export the AidesVeloEngine class and the Aide type.

export { AidesVeloEngine } from "./lib/AidesVeloEngine";
Expand Down
7 changes: 3 additions & 4 deletions src/lib/AidesVeloEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
RuleName,
rules,
} from "..";
import { Situation } from "../../build";
import { Situation } from "../../publicodes-build";
import { slugify } from "./utils";

/**
* Represents an aid with its metadata.
Expand Down Expand Up @@ -226,9 +227,7 @@ export class AidesVeloEngine {
* @returns The commune if found, `undefined` otherwise.
*/
static getCommuneByName(name: string): Commune | undefined {
return data.communes.find(
({ nom }) => nom.toLowerCase() === name.toLowerCase(),
);
return data.communes.find(({ slug }) => slug === slugify(name));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Slugify a string by removing accents, converting to lowercase, and replacing
* spaces with hyphens.
*
* @note It's used to generate the slugified name of the communes.
*
* Source: https://byby.dev/js-slugify-string
*/
export function slugify(str: string): string {
return str
.normalize("NFKD") // split accented characters into their base characters and diacritical marks
.replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
.trim() // trim leading or trailing whitespace
.replace("œ", "oe") // Remove invalid chars
.toLowerCase() // convert to lowercase
.replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
.replace(/\s+/g, "-") // replace spaces with hyphens
.replace(/-+/g, "-"); // remove consecutive hyphens
}
10 changes: 10 additions & 0 deletions test/aidesVeloEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe("AidesVeloEngine", () => {
});
});

describe("getCommuneByName()", () => {
it("should managed to find a commune by its name even if not slugified", () => {
expect(AidesVeloEngine.getCommuneByName("Paris")?.nom).toEqual("Paris");
expect(AidesVeloEngine.getCommuneByName("paris")?.nom).toEqual("Paris");
expect(
AidesVeloEngine.getCommuneByName("SENNECEY LES DIJON")?.nom,
).toEqual("Sennecey-lès-Dijon");
});
});

describe("getAllAidesIn()", () => {
it("should return all aids in France by default", () => {
const engine = globalTestEngine.shallowCopy();
Expand Down

0 comments on commit 71fc88c

Please sign in to comment.