-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️(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)
- Loading branch information
1 parent
f11bcea
commit 0cf4960
Showing
1 changed file
with
12 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = /^[/](?<roomId>[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 = /^[/](?<roomId>[a-z]{3}-[a-z]{4}-[a-z]{3})$/ |