-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
35 lines (28 loc) · 818 Bytes
/
users.js
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
27
28
29
30
31
32
33
34
35
const users = [];
const addUser = ({ id, username, room }) => {
username = username.trim().toLowerCase();
room = room.trim().charAt(0).toUpperCase()+room.slice(1).toLowerCase()
const existingUser = users.find(
(user) => user.username === username && user.room === room
);
if (existingUser) {
return { err: "Username taken" };
}
const user = { id, username, room };
users.push(user);
return { user };
};
const removeUser = (id) => {
const userIndex = users.findIndex((user) => user.id === id);
if (userIndex !== -1) {
return users.splice(userIndex, 1)[0];
}
};
const getUser = (id) => users.find((user) => user.id === id);
const getUsersInRoom = (room) => users.filter((user) => user.room === room);
module.exports = {
addUser,
removeUser,
getUser,
getUsersInRoom,
};