Skip to content

Commit

Permalink
Increase fuzzines of search results (#187)
Browse files Browse the repository at this point in the history
* Introduce normalizeString fn

* Prettier

---------

Co-authored-by: Sebastien Castiel <[email protected]>
  • Loading branch information
sbehrends and scastiel authored Aug 2, 2024
1 parent e990e00 commit 7145cb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
}

0 comments on commit 7145cb6

Please sign in to comment.