Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. const addUser = ({ id, username, room }) => {
  2. // Clean the data
  3. username = username.trim().toLowerCase();
  4. room = room.trim().toLowerCase();
  5.  
  6. // Validate the data
  7. if (!username || !room) {
  8. return {
  9. error: "Username and room required!"
  10. };
  11. }
  12.  
  13. // Check for existing user
  14. const existingUser = users.find(user => {
  15. return user.room === room && user.username === username;
  16. });
  17.  
  18. // Validate username
  19. if (existingUser) {
  20. return {
  21. error: "Username is taken!"
  22. };
  23. }
  24.  
  25. // Store user
  26. const user = { id, username, room };
  27. users.push(user);
  28. console.log(users);
  29. return { user };
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement