b243cc3b8b
Ingredient-alias matching (pantry, can-cook, auto-deduct, shopping-list generation) now covers common bilingual EN/FR ingredients across all 8 grocery categories, up from ~10 staples. Also: matching now ignores accents as well as case (NFD-normalize + strip combining marks) in both the alias resolver (lib/ingredient-match.ts) and the older name-fallback matcher (pantry-shopping-match.ts) — "café"/"Café"/"cafe" and "Épinard"/"epinard" all recognize as the same ingredient without needing every accent variant manually listed as an alias. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { resolveIngredientKey, type IngredientAliasIndex } from "./ingredient-match";
|
|
|
|
export const EXPIRING_WITHIN_DAYS = 3;
|
|
|
|
export function isExpiringSoon(expiresAt: Date | null): boolean {
|
|
if (!expiresAt) return false;
|
|
const days = Math.ceil((expiresAt.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
|
|
return days >= 0 && days <= EXPIRING_WITHIN_DAYS;
|
|
}
|
|
|
|
type ScorableRecipe<T> = T & { ingredients: { rawName: string }[] };
|
|
|
|
export function scoreRecipesAgainstPantry<T>(
|
|
recipesList: ScorableRecipe<T>[],
|
|
pantry: { rawName: string; expiresAt: Date | null }[],
|
|
aliasIndex?: IngredientAliasIndex
|
|
) {
|
|
// With no alias index, this resolves to a plain case/accent-insensitive
|
|
// compare — same behavior as before aliases existed, just accent-aware.
|
|
const keyOf = (name: string) =>
|
|
aliasIndex ? resolveIngredientKey(name, aliasIndex) : name.trim().toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "");
|
|
|
|
const pantryKeys = new Set(pantry.map((p) => keyOf(p.rawName)));
|
|
const expiringSoonKeys = new Set(
|
|
pantry.filter((p) => isExpiringSoon(p.expiresAt)).map((p) => keyOf(p.rawName))
|
|
);
|
|
|
|
return recipesList
|
|
.filter((r) => r.ingredients.length > 0)
|
|
.map((recipe) => {
|
|
const matched = recipe.ingredients.filter((ing) => pantryKeys.has(keyOf(ing.rawName))).length;
|
|
const missing = recipe.ingredients
|
|
.filter((ing) => !pantryKeys.has(keyOf(ing.rawName)))
|
|
.map((ing) => ing.rawName)
|
|
.slice(0, 5);
|
|
const usesExpiring = recipe.ingredients
|
|
.filter((ing) => expiringSoonKeys.has(keyOf(ing.rawName)))
|
|
.map((ing) => ing.rawName);
|
|
const total = recipe.ingredients.length;
|
|
return { recipe, matched, total, pct: Math.round((matched / total) * 100), missing, usesExpiring };
|
|
})
|
|
.sort((a, b) => {
|
|
if (a.usesExpiring.length > 0 !== b.usesExpiring.length > 0) {
|
|
return a.usesExpiring.length > 0 ? -1 : 1;
|
|
}
|
|
return b.pct - a.pct;
|
|
});
|
|
}
|