Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

utils.ts

43 lines1.5 KB

1import { CommandId, MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { type CxOptions, cx } from "class-variance-authority";
3import * as Encoding from "effect/Encoding";
4import { twMerge } from "tailwind-merge";
5import { DraftId } from "../composerDraftStore";
6​
7export function cn(...inputs: CxOptions) {
8 return twMerge(cx(inputs));
9}
10​
11export function isMacPlatform(platform: string): boolean {
12 return /mac|iphone|ipad|ipod/i.test(platform);
14​
15export function isWindowsPlatform(platform: string): boolean {
16 return /^win(dows)?/i.test(platform);
18​
19export function isLinuxPlatform(platform: string): boolean {
20 return /linux/i.test(platform);
22​
23export function randomHex(byteLength: number): string {
24 return Encoding.encodeHex(globalThis.crypto.getRandomValues(new Uint8Array(byteLength)));
26​
27export function randomUUID(): string {
28 const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
29 bytes[6] = (bytes[6]! & 0x0f) | 0x40;
30 bytes[8] = (bytes[8]! & 0x3f) | 0x80;
31 const hex = Encoding.encodeHex(bytes);
32 return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
34​
35export const newCommandId = (): CommandId => CommandId.make(randomUUID());
36​
37export const newProjectId = (): ProjectId => ProjectId.make(randomUUID());
38​
39export const newThreadId = (): ThreadId => ThreadId.make(randomUUID());
40​
41export const newDraftId = (): DraftId => DraftId.make(randomUUID());
42​
43export const newMessageId = (): MessageId => MessageId.make(randomUUID());