MCPcopy Create free account
hub / github.com/codemistic/Web-Development / addUser

Function addUser

Chat_app/src/utils/users.js:3–31  ·  view source on GitHub ↗
({ id, username, room })

Source from the content-addressed store, hash-verified

1const users = []
2
3const addUser = ({ id, username, room }) => {
4 // Clean the user data
5 username = username.trim().toLowerCase()
6 room = room.trim().toLowerCase()
7
8 // Validate the user data
9 if (!username || !room) {
10 return {
11 error: 'Username and room are required!'
12 }
13 }
14
15 // Check for existing user
16 const existingUser = users.find((user) => {
17 return user.room === room && user.username === username
18 })
19
20 // Validate username
21 if (existingUser) {
22 return {
23 error: 'Username is in use!'
24 }
25 }
26
27 // Store user
28 const user = { id, username, room }
29 users.push(user)
30 return { user }
31}
32
33const removeUser = (id) => {
34 const index = users.findIndex((user) => user.id === id)

Callers 1

index.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected