Spaces:
Running
on
T4
Running
on
T4
File size: 1,503 Bytes
2bd3674 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import {random} from 'lodash';
// const USABLE_CHARACTERS = 'BCDFGHJKMPQRTVWXY2346789';
const USABLE_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const ID_LENGTH = 4;
export function isValidRoomID(id: string | null | undefined): boolean {
if (id == null) {
return false;
}
if (id.length !== ID_LENGTH) {
return false;
}
return isValidPartialRoomID(id);
}
export function isValidPartialRoomID(roomID: string): boolean {
return (
roomID.length <= ID_LENGTH &&
roomID.split('').every((char) => USABLE_CHARACTERS.includes(char))
);
}
export default function generateNewRoomID(): string {
return Array.from(
{length: ID_LENGTH},
() => USABLE_CHARACTERS[random(USABLE_CHARACTERS.length - 1)],
).join('');
}
export function getSequentialRoomIDForTestingGenerator(): () => string {
let counter = 0;
return function generateNextRoomID(): string {
const counterInBase: string = Number(counter)
.toString(USABLE_CHARACTERS.length)
.padStart(ID_LENGTH, '0');
if (counterInBase.length > ID_LENGTH) {
throw new Error(
'Ran out of unique room IDs from the sequential generator',
);
}
const result = counterInBase
.split('')
.map(
(digit) => USABLE_CHARACTERS[parseInt(digit, USABLE_CHARACTERS.length)],
)
.join('');
counter++;
return result;
};
}
// const generator = getSequentialRoomIDForTestingGenerator();
// Array.from({length: 200}, () => console.log(generator()));
|