random game IDs

This commit is contained in:
Gavin McDonald
2025-04-09 12:11:51 -04:00
parent 3c4b8cf56e
commit 4a802b2d06
4 changed files with 41 additions and 33 deletions

View File

@@ -1,12 +0,0 @@
export default function getRandomItems(items, count) {
const shuffled = [...items];
// Fisher-Yates shuffle
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
}

View File

@@ -1,8 +1,10 @@
import { customAlphabet } from 'nanoid';
import getRandomItems from '@/tools/getRandomItems';
const alphabet =
'0123456789abcdefghijklmnopqrstuvwxyz';
const generateID = (length: number = 6) => customAlphabet(alphabet, length);
const generateID = (length: number = 6) => {
return getRandomItems(alphabet.split(''), length).join('');
};
export default generateID;