Newer
Older
import { ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
// tailwindcss classnames generator from shadcn
export function cn(...inputs: ClassValue[]) {

Yusuf Akgül
committed
return twMerge(clsx(inputs))

Yusuf Akgül
committed
// changes the default size of the image to be fetched
export function getImageURL(hashId: string, size: string): string {

Yusuf Akgül
committed
const IGDB_IMG_BASE_URL = env.IGDB_IMG_BASE_URL ?? ''
return `${IGDB_IMG_BASE_URL}/t_${size}/${hashId}.jpg`

Yusuf Akgül
committed
}
// calculates the offset for the query
export function calculateOffset(page: number, limit: number): number {

Yusuf Akgül
committed
return (page - 1) * limit

Yusuf Akgül
committed
const date = new Date(data)
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
})

Yusuf Akgül
committed
// formats the time elapsed since creation

Yusuf Akgül
committed
const now = dayjs()
const timeDiff = Math.abs(now.diff(dayjs(createdAt))) // Difference in milliseconds
const seconds = Math.floor(timeDiff / 1000) // Convert to seconds
const minutes = Math.floor(seconds / 60) // Convert to minutes
const hours = Math.floor(minutes / 60) // Convert to hours
const days = Math.floor(hours / 24) // Convert to days

Yusuf Akgül
committed
if (days > 0) {
return dayjs(createdAt).format('MMM D') // Show the date if days have passed
} else if (hours > 0) {
return hours + 'h' // Show hours if hours have passed
} else if (minutes > 0) {
return minutes + 'm' // Show minutes if minutes have passed
} else {
return seconds + 's' // Show seconds if seconds have passed
}

Yusuf Akgül
committed
// gets the current url for server or client

Yusuf Akgül
committed
const IS_SERVER = typeof window === "undefined"

Yusuf Akgül
committed
export default function getURL(path: string) {

Yusuf Akgül
committed
const baseURL = IS_SERVER
? env.NEXT_PUBLIC_APP_URL!
: window.location.origin
return new URL(path, baseURL).toString()
}
export function checkURL(value: string) {
if (!value) return true
const pattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', // fragment locator
'i'
)
try {
const url = new URL(`${(value.startsWith("http://") || value.startsWith("https://")) ? value : `http://${value}`}`)
return pattern.test(url.href)
} catch {
return false
}