mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-09 09:06:13 +01:00
* Introduce normalizeString fn * Prettier --------- Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { Category } from '@prisma/client'
|
|
import { clsx, type ClassValue } from 'clsx'
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
export function delay(ms: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
}
|
|
|
|
export type DateTimeStyle = NonNullable<
|
|
ConstructorParameters<typeof Intl.DateTimeFormat>[1]
|
|
>['dateStyle']
|
|
export function formatDate(
|
|
date: Date,
|
|
options: { dateStyle?: DateTimeStyle; timeStyle?: DateTimeStyle } = {},
|
|
) {
|
|
return date.toLocaleString('en-GB', {
|
|
...options,
|
|
timeZone: 'UTC',
|
|
})
|
|
}
|
|
|
|
export function formatCategoryForAIPrompt(category: Category) {
|
|
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
|
|
}
|
|
|
|
export function formatCurrency(currency: string, amount: number) {
|
|
const format = new Intl.NumberFormat('en-US', {
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2,
|
|
})
|
|
const formattedAmount = format.format(amount / 100)
|
|
return `${currency} ${formattedAmount}`
|
|
}
|
|
|
|
export function formatFileSize(size: number) {
|
|
const formatNumber = (num: number) =>
|
|
num.toLocaleString('en-US', {
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 1,
|
|
})
|
|
|
|
if (size > 1024 ** 3) return `${formatNumber(size / 1024 ** 3)} GB`
|
|
if (size > 1024 ** 2) return `${formatNumber(size / 1024 ** 2)} MB`
|
|
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, '')
|
|
}
|