// Ids for blocks, sections, suggestions and comments: sortable by time, unique // enough across collaborators, and made only of characters that are safe in // record keys and DOM ids. const ALPHA = '234567abcdefghijklmnopqrstuvwxyz'; let last = 0; let clock = 0; function base32(n: number, width: number): string { let out = ''; for (let i = 0; i < width; i++) { out = ALPHA[n % 32] + out; n = Math.floor(n / 32); } return out; } /** 13 characters: a microsecond-ish timestamp then two random characters. */ export function newId(): string { let t = Date.now() * 1000; if (t <= last) t = last + 1; last = t; clock = (clock + 1) % 1024; const rnd = Math.floor(Math.random() * 1024); return base32(t, 11) + base32(rnd ^ clock, 2); }