From 174009eba159ef112d943963719a82c6b369a8ab Mon Sep 17 00:00:00 2001 From: XuhuiZhou Date: Sun, 15 Sep 2024 01:21:43 -0400 Subject: [PATCH] fix bugs --- lib/utils.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/utils.ts b/lib/utils.ts index bd0c391..6e052c9 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,6 +1,36 @@ import { clsx, type ClassValue } from "clsx" import { twMerge } from "tailwind-merge" +import { customAlphabet } from "nanoid"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +// 7-character random string +export const nanoid = customAlphabet( + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", + 7, +); + +export function nFormatter(num: number, digits?: number) { + if (!num) return "0"; + const lookup = [ + { value: 1, symbol: "" }, + { value: 1e3, symbol: "K" }, + { value: 1e6, symbol: "M" }, + { value: 1e9, symbol: "G" }, + { value: 1e12, symbol: "T" }, + { value: 1e15, symbol: "P" }, + { value: 1e18, symbol: "E" }, + ]; + const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; + var item = lookup + .slice() + .reverse() + .find(function (item) { + return num >= item.value; + }); + return item + ? (num / item.value).toFixed(digits || 1).replace(rx, "$1") + item.symbol + : "0"; +} \ No newline at end of file