Created
April 24, 2024 05:06
-
-
Save rifayetuxbd/11f486882fd39f657467c8d10bc49847 to your computer and use it in GitHub Desktop.
Next js utils functions under /lib/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { clsx, type ClassValue } from "clsx" | |
import { customAlphabet } from "nanoid" | |
import { twMerge } from "tailwind-merge" | |
export function cn(...inputs: ClassValue[]) { | |
return twMerge(clsx(inputs)) | |
} | |
export function generateId(length: number = 16) { | |
return customAlphabet( | |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | |
length | |
)() | |
} | |
export function formatPrice( | |
price: number | string, | |
options: Intl.NumberFormatOptions = {}, | |
locales: Intl.LocalesArgument = "en-US" | |
) { | |
return new Intl.NumberFormat(locales, { | |
style: "currency", | |
currency: options.currency ?? "BDT", | |
notation: options.notation ?? "standard", | |
...options, | |
}).format(Number(price)) | |
} | |
export function formatNumber( | |
number: number | string, | |
options: Intl.NumberFormatOptions = {}, | |
locales: Intl.LocalesArgument = "en-US" | |
) { | |
return new Intl.NumberFormat(locales, { | |
style: options.style ?? "decimal", | |
notation: options.notation ?? "standard", | |
minimumFractionDigits: options.minimumFractionDigits ?? 0, | |
maximumFractionDigits: options.maximumFractionDigits ?? 2, | |
...options, | |
}).format(Number(number)) | |
} | |
export function formatDate( | |
date: Date | string | number, | |
options: Intl.DateTimeFormatOptions = {}, | |
locales: Intl.LocalesArgument = "en-US" | |
) { | |
return new Intl.DateTimeFormat(locales, { | |
month: options.month ?? "long", | |
day: options.day ?? "numeric", // "2-digit" | "numeric" | |
year: options.year ?? "numeric", | |
...options, | |
}).format(new Date(date)) | |
} | |
export function formatBytes( | |
bytes: number, | |
decimals = 0, | |
sizeType: "accurate" | "normal" = "normal" | |
) { | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB"] | |
const accurateSizes = ["Bytes", "KiB", "MiB", "GiB", "TiB"] | |
if (bytes === 0) return "0 Byte" | |
const i = Math.floor(Math.log(bytes) / Math.log(1024)) | |
return `${(bytes / Math.pow(1024, i)).toFixed(decimals)} ${ | |
sizeType === "accurate" ? accurateSizes[i] ?? "Bytest" : sizes[i] ?? "Bytes" | |
}` | |
} | |
export function formatId(id: string) { | |
return `#${id.toString().padStart(4, "0")}` | |
} | |
export function slugify(str: string) { | |
return str | |
.toLowerCase() | |
.replace(/ /g, "-") | |
.replace(/[^\w-]+/g, "") | |
.replace(/--+/g, "-") | |
} | |
export function unslugify(str: string) { | |
return str.replace(/-/g, " ") | |
} | |
export function toTitleCase(str: string) { | |
return str.replace( | |
/\w\S*/g, | |
(txt) => txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase() | |
) | |
} | |
export function toSentenceCase(str: string) { | |
return str | |
.replace(/([A-Z])/g, " $1") | |
.replace(/^./, (str) => str.toUpperCase()) | |
} | |
export function truncate(str: string, length: number) { | |
return str.length > length ? `${str.substring(0, length)}...` : str | |
} | |
export function isMacOs() { | |
if (typeof window === "undefined") return false | |
return window.navigator.userAgent.includes("Mac") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment