feat: add Svelte actions and global stores for enhanced functionality

This commit is contained in:
2026-02-25 13:10:52 +00:00
parent dc00d24899
commit f6f565bbcb
7 changed files with 262 additions and 6 deletions
+59
View File
@@ -0,0 +1,59 @@
export function debounce<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout | 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, "")
}