github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/util/randomId.ts (about) 1 /** 2 * generates a random ID to be used outside react-land (eg jquery) 3 * it's mandatory to generate it once, preferrably on a function's body 4 * IMPORTANT: it does NOT: 5 * - generate unique ids across server/client 6 * use `useId` instead (https://reactjs.org/docs/hooks-reference.html#useid) 7 * - guarantee no collisions will happen (although it's unlikely) 8 */ 9 export function randomId(prefix?: string) { 10 const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 11 const num = 5; 12 13 const id = Array(num) 14 .fill(0) 15 .map(() => letters.substr(Math.floor(Math.random() * num + 1), 1)) 16 .join(''); 17 18 if (prefix) { 19 return `${prefix}-${id}`; 20 } 21 22 return id; 23 }