utils.ts
1 import { md5 } from "@noble/hashes/legacy.js"; 2 import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js"; 3 4 /** 5 * Converts a UTF-8 string into an MD5 HEX. 6 * 7 * @example 8 * md5hex("helloworld"); // "fc5e038d38a57032085441e7fe7010b0" 9 */ 10 export const md5hex = (input: string) => bytesToHex(md5(utf8ToBytes(input))); 11 12 /** 13 * Removes query parameters from a string, typically a URL. 14 * 15 * @example 16 * removeQueryParams("https://example.com/path?query=param"); // "https://example.com/path" 17 */ 18 export const removeQueryParams = (input: string) => { 19 return input || typeof input === "string" ? input.split("?")[0] : null; 20 }; 21 22 /** 23 * Returns a number if the input is a number, otherwise returns null. 24 * 25 * @example 26 * numberOrNull(42); // 42 27 * numberOrNull("not a number"); // null 28 */ 29 export const numberOrNull = (num: unknown): number | null => { 30 return typeof num === "number" ? num : null; 31 };