web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

index.ts (955B)


      1 import { parse, v4, v7 } from "uuid";
      2 
      3 export const uuidv4 = () => v4();
      4 export const uuidv7 = () => v7();
      5 
      6 const bytes_to_b64 = (bytes: Uint8Array): string => {
      7     if (typeof btoa === "undefined") throw new Error("btoa_undefined");
      8     const chars: string[] = new Array(bytes.length);
      9     for (let i = 0; i < bytes.length; i++) chars[i] = String.fromCharCode(bytes[i]);
     10     return btoa(chars.join(""));
     11 };
     12 
     13 const b64_to_b64url = (value: string): string =>
     14     value.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
     15 
     16 export const uuidv4_b64url = (): string => {
     17     const uuid_val = uuidv4();
     18     const bytes = parse(uuid_val);
     19     const b64 = bytes_to_b64(bytes);
     20     return b64_to_b64url(b64);
     21 };
     22 
     23 export const uuidv7_b64url = (): string => {
     24     const uuid_val = uuidv7();
     25     const bytes = parse(uuid_val);
     26     const b64 = bytes_to_b64(bytes);
     27     return b64_to_b64url(b64);
     28 };
     29 
     30 export const d_tag_create = (): string => uuidv7_b64url();