63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
export function debounce<T extends (...args: never[]) => void>(
|
|
func: T,
|
|
wait: number
|
|
): (...args: Parameters<T>) => void {
|
|
let timeout: ReturnType<typeof setTimeout> | null = null
|
|
|
|
return (...args: Parameters<T>) => {
|
|
if (timeout) {
|
|
clearTimeout(timeout)
|
|
}
|
|
timeout = setTimeout(() => func(...args), wait)
|
|
}
|
|
}
|
|
|
|
export function formatNumber(number: number, decimals = 2): string {
|
|
return number.toFixed(decimals)
|
|
}
|
|
|
|
export function formatCurrency(amount: number, currency = "EUR", locale = "de-DE"): string {
|
|
// goja (tibi-server SSR runtime) does not provide Intl
|
|
if (typeof Intl === "undefined") {
|
|
const fixed = amount.toFixed(2).replace(".", ",")
|
|
return `${fixed}\u00A0${currency === "EUR" ? "€" : currency}`
|
|
}
|
|
return new Intl.NumberFormat(locale, {
|
|
style: "currency",
|
|
currency: currency,
|
|
}).format(amount)
|
|
}
|
|
|
|
export function generateMockImage(seed: string): string {
|
|
// Generate a consistent color based on the seed
|
|
const colors = [
|
|
"bg-red-200",
|
|
"bg-blue-200",
|
|
"bg-green-200",
|
|
"bg-yellow-200",
|
|
"bg-purple-200",
|
|
"bg-pink-200",
|
|
"bg-indigo-200",
|
|
"bg-teal-200",
|
|
]
|
|
|
|
const colorIndex = seed.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0) % colors.length
|
|
return colors[colorIndex]
|
|
}
|
|
|
|
export function highlightSearchTerm(text: string, searchTerm: string): string {
|
|
if (!searchTerm.trim()) return text
|
|
|
|
const regex = new RegExp(`(${searchTerm})`, "gi")
|
|
return text.replace(regex, '<mark class="bg-yellow-200">$1</mark>')
|
|
}
|
|
|
|
export function slugify(value: string): string {
|
|
return value
|
|
.normalize("NFD")
|
|
.replace(/[\u0300-\u036f]/g, "")
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, "-")
|
|
.replace(/(^-|-$)/g, "")
|
|
}
|