Files
kolaytercih/src/lib/arama.ts

31 lines
952 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Katalog aramasının paylaşılan metin eşleştirme yardımcıları.
* Hem /api/katalog-ara route'u hem de client'taki rehber araması kullanır —
* iki taraf aynı normalize kurallarıyla eşleşmeli.
*/
export function aramaNormalize(metin: string): string {
return metin
.toLocaleLowerCase("tr-TR")
.replace(/ı/g, "i")
.replace(/ş/g, "s")
.replace(/ğ/g, "g")
.replace(/ü/g, "u")
.replace(/ö/g, "o")
.replace(/ç/g, "c")
.replace(/â/g, "a")
.replace(/î/g, "i")
.replace(/û/g, "u")
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "");
}
/** 0 = tam eşleşme, 1 = baş, 2 = kelime başı, 3 = geçiyor */
export function eslesmePuani(ad: string, sorgu: string): number {
const normalAd = aramaNormalize(ad);
if (normalAd === sorgu) return 0;
if (normalAd.startsWith(sorgu)) return 1;
if (normalAd.split(/\s+/).some((kelime) => kelime.startsWith(sorgu))) return 2;
return 3;
}