-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
40 lines (34 loc) · 1.04 KB
/
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
36
37
38
39
40
const users = [];
let usersInTheRoom = [];
const addUser = (data) => {
let { id, name, room } = data;
userName = name.trim().toLowerCase();
usersRoom = room.trim().toLowerCase();
const existingUser = users.find(
(user) => user.room === usersRoom && user.name === userName
);
if (existingUser) {
return { error: "Username is already taken" };
}
const user = { id, userName, usersRoom };
users.push(user);
return { user };
};
const removeUser = (id) => {
const currUserIndex = users.findIndex((user) => user.id === id);
return currUserIndex !== -1 ? users.splice(currUserIndex, 1)[0] : null;
// if (currUserIndex !== -1) {
// return users.splice(currUserIndex, 1)[0];
// }
};
const getUser = (id) => {
const userFound = users.find((user) => user.id === id);
return userFound;
};
const getUsersInRoom = (room) => {
usersInTheRoom = users
.filter((user) => user.usersRoom === room)
.map((n) => n.userName);
return usersInTheRoom;
};
module.exports = { addUser, removeUser, getUser, getUsersInRoom };