code-share/lib/utils.ts

36 lines
976 B
TypeScript
Raw Normal View History

2024-02-20 06:52:39 +00:00
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
2024-02-21 02:01:35 +07:00
import { BASE_URL } from "./consts";
2024-02-22 20:59:20 +00:00
import type { ProjectSchema } from "~/server/db/schema/project";
import type { FileSchema } from "~/server/db/schema/file";
2024-02-18 21:14:41 +07:00
export function cn(...inputs: ClassValue[]) {
2024-02-20 06:52:39 +00:00
return twMerge(clsx(inputs));
}
export function getFileExt(filename: string) {
return filename.substring(filename.lastIndexOf(".") + 1);
2024-02-18 21:14:41 +07:00
}
2024-02-21 02:01:35 +07:00
export function getUrl(...path: string[]) {
return BASE_URL + ("/" + path.join("/")).replace(/\/\/+/g, "/");
}
2024-02-22 20:59:20 +00:00
export function getPreviewUrl(
project: Pick<ProjectSchema, "slug">,
file: string | Pick<FileSchema, "path">
) {
return getUrl(
`api/preview/${project.slug}`,
typeof file === "string" ? file : file.path
);
}
2024-02-23 19:36:10 +07:00
export const ucfirst = (str: string) => {
return str.charAt(0).toUpperCase() + str.substring(1);
};
export const ucwords = (str: string) => {
return str.split(" ").map(ucfirst).join(" ");
};