panku-app/src/utils.js

1import { config } from "./config"; 2 3export const logger = (...data) => { 4 if (config.buildMode === "development") { 5 console.log(...data); 6 } 7 return; 8}; 9 10export const safeParseJSON = (str) => { 11 let json = null; 12 try { 13 if (typeof str === "string") { 14 json = JSON.parse(str); 15 } 16 } catch (error) { 17 logger(error); 18 } 19 return json; 20}; 21 22export const makeId = () => { 23 let id = Math.random().toString(36).substring(2); // p0zob1fwh6 24 25 if (crypto && crypto.randomUUID) { 26 id = crypto.randomUUID(); 27 } else { 28 // for non https, crypto wont be available 29 id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { 30 const r = (Math.random() * 16) | 0; 31 const v = c === "x" ? r : (r & 0x3) | 0x8; 32 return v.toString(16); 33 }); 34 } 35 return id; 36}; 37 38export const formatDate = (dateString) => { 39 const dateObj = new Date(dateString); 40 41 const formattedDate = dateObj.toLocaleDateString("en-GB", { 42 day: "numeric", 43 month: "short", 44 year: "numeric", 45 }); 46 47 return formattedDate; 48};