我們在 Day 9 已完成基本搜尋。今天做體驗升級:全形/半形統一、忽略多餘空白、簡易模糊比對(包含順序相近也可命中)。
改哪裡:js/app.js(applyFilters())
function norm(s){
return s.toString()
.toLowerCase()
.replace(/\s+/g, '') // 去空白
.replace(/[A-Za-z0-9]/g, ch => String.fromCharCode(ch.charCodeAt(0) - 0xFEE0)); // 全形→半形
}
// 簡易模糊:關鍵字中每個連續字母都出現在目標(順序保留)
function fuzzyIncludes(target, key){
if (!key) return true;
let i = 0, j = 0;
while (i < target.length && j < key.length){
if (target[i] === key[j]) j++;
i++;
}
return j === key.length;
}
function applyFilters(){
const active = new Set(getActiveGenres());
const kw = norm(keyword.trim());
return SHOWS.filter(s=>{
const byGenre = active.size ? s.genres.some(g => active.has(g)) : true;
const titleN = norm(s.title);
const byKw = kw ? (titleN.includes(kw) || fuzzyIncludes(titleN, kw)) : true;
return byGenre && byKw;
});
}