Skip to content

Commit

Permalink
fix: Correction de la fonction de formattage de numéro de téléphone +…
Browse files Browse the repository at this point in the history
… tests (#151)
  • Loading branch information
ggounot authored Jan 6, 2025
1 parent d391a82 commit 66c585f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
15 changes: 7 additions & 8 deletions front/src/lib/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,13 @@ export function arraysCompare(a, b) {
}

export function formatPhoneNumber(phoneNumber: string): string {
let result = "";
phoneNumber.split("").forEach((char, i) => {
if (i !== 0 && i % 2 === 0) {
result += " ";
}
result += char;
});
return result;
// Supprime tous les espaces
const cleanedNumber = phoneNumber.replace(/\s+/g, "");

// Découpe en groupes de 2 chiffres
const formattedNumber = cleanedNumber.match(/\d{1,2}/g)?.join(" ") || "";

return formattedNumber;
}

export function isInDeploymentDepartments(
Expand Down
28 changes: 28 additions & 0 deletions front/src/tests/utils/misc/formatPhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, test } from "vitest";
import { formatPhoneNumber } from "$lib/utils/misc";

describe("formatPhoneNumber", () => {
test("formate correctement un numéro de téléphone avec des espaces", () => {
expect(formatPhoneNumber("06 12 34 56 78")).toBe("06 12 34 56 78");
});

test("supprime les espaces supplémentaires et formate correctement", () => {
expect(formatPhoneNumber("06 12 34 56 78")).toBe("06 12 34 56 78");
});

test("gère un numéro sans espaces", () => {
expect(formatPhoneNumber("0612345678")).toBe("06 12 34 56 78");
});

test("gère un numéro avec un nombre impair de chiffres", () => {
expect(formatPhoneNumber("06123456789")).toBe("06 12 34 56 78 9");
});

test("retourne une chaîne vide pour une entrée vide", () => {
expect(formatPhoneNumber("")).toBe("");
});

test("ignore les caractères non numériques", () => {
expect(formatPhoneNumber("06a12b34c56d78")).toBe("06 12 34 56 78");
});
});

0 comments on commit 66c585f

Please sign in to comment.