-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-utils.js
More file actions
26 lines (21 loc) · 783 Bytes
/
user-utils.js
File metadata and controls
26 lines (21 loc) · 783 Bytes
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
let currentUsers = [];
// socket id should be unique so there should be no need to check other values
const addUser = ({user, id, room}) => {
const existingUser = currentUsers.find((user)=> user.id===id );
if (existingUser) {
return { error: 'Username is taken'};
}
currentUsers.push({user, id, room});
console.log(currentUsers)
}
const removeUser = (id) => {
const info = currentUsers.find((user)=> user.id===id);
const index = currentUsers.indexOf(info);
if(index!==-1)
{
currentUsers.splice(index, 1)[0];
}
}
const getUserAndRoom = (id) => currentUsers.find((user) => user.id===id);
// const getUsersInRoom = (room) => users.filter((user) => user.room == room);
module.exports = {addUser, removeUser, getUserAndRoom};