Increase fuzzines of search results (#187)

* Introduce normalizeString fn

* Prettier

---------

Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
This commit is contained in:
Sergio Behrends
2024-08-02 16:57:18 +02:00
committed by GitHub
parent e990e00a75
commit 7145cb6f30
2 changed files with 15 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import { getGroupExpensesAction } from '@/app/groups/[groupId]/expenses/expense-
import { Button } from '@/components/ui/button'
import { SearchBar } from '@/components/ui/search-bar'
import { Skeleton } from '@/components/ui/skeleton'
import { normalizeString } from '@/lib/utils'
import { Participant } from '@prisma/client'
import dayjs, { type Dayjs } from 'dayjs'
import Link from 'next/link'
@@ -134,13 +135,15 @@ export function ExpenseList({
return expenses.length > 0 ? (
<>
<SearchBar onValueChange={(value) => setSearchText(value)} />
<SearchBar
onValueChange={(value) => setSearchText(normalizeString(value))}
/>
{Object.values(EXPENSE_GROUPS).map((expenseGroup: string) => {
let groupExpenses = groupedExpensesByDate[expenseGroup]
if (!groupExpenses) return null
groupExpenses = groupExpenses.filter(({ title }) =>
title.toLowerCase().includes(searchText.toLowerCase()),
normalizeString(title).includes(searchText),
)
if (groupExpenses.length === 0) return null

View File

@@ -48,3 +48,13 @@ export function formatFileSize(size: number) {
if (size > 1024) return `${formatNumber(size / 1024)} kB`
return `${formatNumber(size)} B`
}
export function normalizeString(input: string): string {
// Replaces special characters
// Input: áäåèéę
// Output: aaaeee
return input
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
}