From 0cf4960969e893c78d26bd295d8a0f23c669818d Mon Sep 17 00:00:00 2001 From: Emmanuel Pelletier Date: Sun, 21 Jul 2024 17:16:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(rooms)=20room=20id=20gen:=20?= =?UTF-8?q?write=20more=20es6-like=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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) --- .../features/rooms/utils/generateRoomId.ts | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/frontend/src/features/rooms/utils/generateRoomId.ts b/src/frontend/src/features/rooms/utils/generateRoomId.ts index 76bce446..a075ad50 100644 --- a/src/frontend/src/features/rooms/utils/generateRoomId.ts +++ b/src/frontend/src/features/rooms/utils/generateRoomId.ts @@ -1,30 +1,16 @@ +// Google Meet uses only letters in a room identifier +const ROOM_ID_ALLOWED_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz' -const getRandomChar = () => { - // Google Meet uses only letters in a room identifier - const characters = 'abcdefghijklmnopqrstuvwxyz'; - const charactersLength = characters.length; - return characters.charAt(Math.floor(Math.random() * charactersLength)) -} +const getRandomChar = () => + ROOM_ID_ALLOWED_CHARACTERS[ + Math.floor(Math.random() * ROOM_ID_ALLOWED_CHARACTERS.length) + ] -const generateSegment = (length: number): string => { - let segment = ''; - for (let i = 0; i < length; i++) { - segment += getRandomChar(); - } - return segment; -}; +const generateSegment = (length: number): string => + Array.from(Array(length), getRandomChar).join('') -export const generateRoomId = () => { - // Generates a unique room identifier following the Google Meet format - const shortLength = 3; - const longLength = 4; - const parts = [ - generateSegment(shortLength), - generateSegment(longLength), - generateSegment(shortLength) - ]; - return parts.join('-'); -} - -export const roomIdRegex = /^[/](?[a-z]{3}-[a-z]{4}-[a-z]{3})$/; +// Generates a unique room identifier following the Google Meet format +export const generateRoomId = () => + [generateSegment(3), generateSegment(4), generateSegment(3)].join('-') +export const roomIdRegex = /^[/](?[a-z]{3}-[a-z]{4}-[a-z]{3})$/