diff --git a/lib/store-db.ts b/lib/store-db.ts index 25589c3..0cc0fb8 100644 --- a/lib/store-db.ts +++ b/lib/store-db.ts @@ -143,9 +143,10 @@ export function autoCategorize(description: string, categories: Category[]): str for (const keyword of category.keywords) { const lowerKeyword = keyword.toLowerCase() - // Pour les keywords courts (< 4 chars), matcher uniquement des mots entiers - if (lowerKeyword.length < 4) { - const wordBoundary = new RegExp(`\\b${lowerKeyword}\\b`) + // Pour les keywords courts (< 6 chars), matcher uniquement des mots entiers + // Évite les faux positifs comme "chat" dans "achat" + if (lowerKeyword.length < 6) { + const wordBoundary = new RegExp(`\\b${escapeRegex(lowerKeyword)}\\b`) if (wordBoundary.test(lowerDesc)) { return category.id } @@ -161,6 +162,11 @@ export function autoCategorize(description: string, categories: Category[]): str return null } +// Échappe les caractères spéciaux pour les regex +function escapeRegex(str: string): string { + return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") +} + export function generateId(): string { return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}` }