mirror of
https://github.com/privacy-scaling-explorations/pse.dev.git
synced 2026-01-12 07:38:20 -05:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { ReadonlyURLSearchParams } from "next/navigation"
|
|
import { clsx, type ClassValue } from "clsx"
|
|
import { twMerge } from "tailwind-merge"
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
export function uniq(arr: any[], removeEmpty = true) {
|
|
const uniqArray = Array.from(new Set(arr))
|
|
|
|
return removeEmpty ? uniqArray.filter(Boolean) : uniqArray
|
|
}
|
|
|
|
export function queryStringToObject(
|
|
searchParams: ReadonlyURLSearchParams
|
|
): Record<string, any> {
|
|
const obj = Object.fromEntries(searchParams.entries())
|
|
const object: Record<string, any> = {}
|
|
Object.keys(obj).forEach((key) => {
|
|
object[key] = obj[key]?.split(",")
|
|
})
|
|
|
|
return object
|
|
}
|
|
|
|
export function shuffleArray<T>(array: T[]) {
|
|
return array.sort(() => 0.5 - Math.random())
|
|
}
|
|
|
|
export function convertDirtyStringToHtml(string: string) {
|
|
const urlPattern =
|
|
/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim
|
|
const pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim
|
|
|
|
if (!string) return ""
|
|
return string
|
|
.replace(/\n/g, "<br />")
|
|
.replace(urlPattern, '<a href="$&">$&</a>')
|
|
.replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')
|
|
.toLowerCase()
|
|
}
|
|
|
|
export function removeProtocol(url: string) {
|
|
return url?.replace(/^https?:\/\//, "")
|
|
}
|