This commit is contained in:
Kim Ravn Hansen
2025-09-04 16:54:03 +02:00
parent fc28f4ef55
commit 0acd46fb6b
16 changed files with 629 additions and 21 deletions

26
server/utils/id.js Normal file
View File

@@ -0,0 +1,26 @@
export function cleanName(s) {
if (typeof s !== "string") {
throw new Error("String expected, but got a ", typeof s);
}
return s.toLowerCase().replace(" ", "_").replace(/[^a-zA-Z0-9_]/, "_");
}
/**
* Generate a random number, convert it to base36, and return it as a string with 7-8 characters.
*/
export function miniUid() {
// we use 12 digits, but we could go up to 16
return Number(Math.random().toFixed(12).substring(2)).toString(36);
}
/**
* Generate an id from a name
*/
export function fromName(...names) {
let res = "";
for (const name of names) {
res += ":" + cleanName(name);
}
return res + ":" + miniUid();
}