Advertisement
viligen

story

Jun 19th, 2022
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Story {
  2.     constructor(title, creator) {
  3.         this.title = title;
  4.         this.creator = creator;
  5.         this._comments = []; // {Id, Username, Content, Replies: [{Id, Username, Content}]}
  6.         this._likes = []; //usernames
  7.     }
  8.     get likes() {
  9.         if (this._likes.length === 0) {
  10.             return `${this.title} has 0 likes`;
  11.         }
  12.         if (this._likes.length === 1) {
  13.             return `${this._likes[0]} likes this story!`;
  14.         }
  15.         return `${this._likes[0]} and ${
  16.             this._likes.length - 1
  17.         } others like this story!`;
  18.     }
  19.     like(username) {
  20.         if (this._likes.includes(username)) {
  21.             throw new Error("You can't like the same story twice!");
  22.         }
  23.         if (this.creator === username) {
  24.             throw new Error("You can't like your own story!");
  25.         }
  26.         this._likes.push(username);
  27.         return `${username} liked ${this.title}!`;
  28.     }
  29.     dislike(username) {
  30.         let idx = this._likes.findIndex((u) => u === username);
  31.         if (idx === -1) {
  32.             throw new Error("You can't dislike this story!");
  33.         }
  34.  
  35.         this._likes.splice(idx, 1);
  36.  
  37.         return `${username} disliked ${this.title}`;
  38.     }
  39.     comment(username, content, id) {
  40.         let existingComment = this._comments.find((c) => c.Id === id);
  41.         if (id === undefined || existingComment === undefined) {
  42.             this._comments.push({
  43.                 Id: this._comments.length + 1,
  44.                 Username: username,
  45.                 Content: content,
  46.                 Replies: [],
  47.             });
  48.             return `${username} commented on ${this.title}`;
  49.         }
  50.         existingComment.Replies.push({
  51.             Id: `${id}.${existingComment.Replies.length + 1}`,
  52.             Username: username,
  53.             Content: content,
  54.         });
  55.         return 'You replied successfully';
  56.     }
  57.     toString(sortingType) {
  58.         let result = [
  59.             `Title: ${this.title}
  60. Creator: ${this.creator}
  61. Likes: ${this._likes.length}
  62. Comments:`,
  63.         ];
  64.         let sorted;
  65.         if (sortingType == 'asc') {
  66.             this._comments
  67.                 .sort((a, b) => a.Id - b.Id)
  68.                 .forEach((c) => c.Replies.sort((a, b) => a.Id - b.Id));
  69.         } else if (sortingType == 'desc') {
  70.             this._comments
  71.                 .sort((a, b) => b.Id - a.Id)
  72.                 .forEach((c) => c.Replies.sort((a, b) => b.Id - a.Id));
  73.         } else if (sortingType == 'username') {
  74.             this._comments
  75.                 .sort((a, b) => a.Username.localeCompare(b.Username))
  76.                 .forEach((c) =>
  77.                     c.Replies.sort((a, b) =>
  78.                         a.Username.localeCompare(b.Username)
  79.                     )
  80.                 );
  81.         }
  82.  
  83.         this._comments.forEach((c) => {
  84.             result.push(`-- ${c.Id}. ${c.Username}: ${c.Content}`);
  85.             c.Replies.forEach((r) =>
  86.                 result.push(`--- ${r.Id}. ${r.Username}: ${r.Content}`)
  87.             );
  88.         });
  89.  
  90.         return result.join('\n');
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement