♻️(rooms) room id gen: write more es6-like code
- this feels a bit less boilerplaty to read - puting the characters whitelist outside the function to prevent creating the var each time (yes, this of super great importance)
This commit is contained in:
@@ -1,30 +1,16 @@
|
|||||||
|
// Google Meet uses only letters in a room identifier
|
||||||
|
const ROOM_ID_ALLOWED_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz'
|
||||||
|
|
||||||
const getRandomChar = () => {
|
const getRandomChar = () =>
|
||||||
// Google Meet uses only letters in a room identifier
|
ROOM_ID_ALLOWED_CHARACTERS[
|
||||||
const characters = 'abcdefghijklmnopqrstuvwxyz';
|
Math.floor(Math.random() * ROOM_ID_ALLOWED_CHARACTERS.length)
|
||||||
const charactersLength = characters.length;
|
]
|
||||||
return characters.charAt(Math.floor(Math.random() * charactersLength))
|
|
||||||
}
|
|
||||||
|
|
||||||
const generateSegment = (length: number): string => {
|
const generateSegment = (length: number): string =>
|
||||||
let segment = '';
|
Array.from(Array(length), getRandomChar).join('')
|
||||||
for (let i = 0; i < length; i++) {
|
|
||||||
segment += getRandomChar();
|
|
||||||
}
|
|
||||||
return segment;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const generateRoomId = () => {
|
// Generates a unique room identifier following the Google Meet format
|
||||||
// Generates a unique room identifier following the Google Meet format
|
export const generateRoomId = () =>
|
||||||
const shortLength = 3;
|
[generateSegment(3), generateSegment(4), generateSegment(3)].join('-')
|
||||||
const longLength = 4;
|
|
||||||
const parts = [
|
|
||||||
generateSegment(shortLength),
|
|
||||||
generateSegment(longLength),
|
|
||||||
generateSegment(shortLength)
|
|
||||||
];
|
|
||||||
return parts.join('-');
|
|
||||||
}
|
|
||||||
|
|
||||||
export const roomIdRegex = /^[/](?<roomId>[a-z]{3}-[a-z]{4}-[a-z]{3})$/;
|
|
||||||
|
|
||||||
|
export const roomIdRegex = /^[/](?<roomId>[a-z]{3}-[a-z]{4}-[a-z]{3})$/
|
||||||
|
|||||||
Reference in New Issue
Block a user