Advertisement
why_where_what

02. Story

Feb 20th, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Story {
  2.     #comments;
  3.     #likes;
  4.  
  5.     constructor(title, creator) {
  6.         this.title = title;
  7.         this.creator = creator;
  8.         this.#comments = [];
  9.         this.#likes = [];
  10.     }
  11.  
  12.     get likes() {
  13.         if (this.#likes.length === 0) {
  14.             return `${this.title} has 0 likes`;
  15.         } else if (this.#likes.length === 1) {
  16.             let username = this.#likes[0];
  17.  
  18.             return `${username} likes this story!`;
  19.         } else {
  20.             let username = this.#likes[0];
  21.             let otherUsersCount = this.#likes.length - 1;
  22.  
  23.             return `${username} and ${otherUsersCount} others like this story!`;
  24.         }
  25.     };
  26.  
  27.     like(username) {
  28.         if (this.#likes.some(x => x === username)) {
  29.             throw new Error('You can\'t like the same story twice!');
  30.         }
  31.  
  32.         if (this.creator === username) {
  33.             throw new Error('You can\'t like your own story!');
  34.         }
  35.  
  36.         this.#likes.push(username);
  37.         return `${username} liked ${this.title}!`;
  38.     };
  39.  
  40.     dislike(username) {
  41.         if (!this.#likes.some(x => x === username)) {
  42.             throw new Error('You can\'t dislike this story!');
  43.         }
  44.  
  45.         let usernameIndex = this.#likes.indexOf(username);
  46.         this.#likes.splice(usernameIndex, 1);
  47.  
  48.         return `${username} disliked ${this.title}`;
  49.     };
  50.  
  51.     comment(username, content, id) {
  52.         if (id === undefined ||
  53.             !this.#comments.some(c => c.id === id)) {
  54.  
  55.             id = this.#comments.length + 1;
  56.  
  57.             this.#comments.push({
  58.                 id,
  59.                 username,
  60.                 content,
  61.                 replies: []
  62.             });
  63.  
  64.             return `${username} commented on ${this.title}`;
  65.         } else {
  66.             let comment = this.#comments.find(c => c.id === id);
  67.  
  68.             if(comment.replies.length === 0) {
  69.                 comment.replies.push({
  70.                     id: `${comment.id}.1`,
  71.                     username,
  72.                     content
  73.                 });
  74.             } else {
  75.                 let lastReply = comment.replies[comment.replies.length - 1];
  76.                 let lastDigitPlusOne = Number(lastReply.id[lastReply.id.length - 1]) + 1;
  77.                 let newId = `${comment.id}.${lastDigitPlusOne}`;
  78.  
  79.                 comment.replies.push({
  80.                     id: newId,
  81.                     username,
  82.                     content
  83.                 });
  84.             }
  85.  
  86.             return 'You replied successfully';
  87.         }
  88.     };
  89.  
  90.     toString(sortingType) {
  91.         let result = [];
  92.         result.push(`Title: ${this.title}`);
  93.         result.push(`Creator: ${this.creator}`);
  94.         result.push(`Likes: ${this.#likes.length}`);
  95.         result.push(`Comments:`);
  96.  
  97.         let comments = this.#comments.slice();
  98.  
  99.         if (sortingType === 'asc') {
  100.             comments.sort((a, b) => {
  101.                 return a.id.toString().toLowerCase().localeCompare(b.id.toString().toLowerCase());
  102.             })
  103.         } else if (sortingType === 'desc') {
  104.             comments.sort((a, b) => {
  105.                 return b.id.toString().toLowerCase().localeCompare(a.id.toString().toLowerCase());
  106.             })
  107.         } else if (sortingType === 'username') {
  108.             comments.sort((a, b) => {
  109.                 return a.username.toLowerCase().localeCompare(b.username.toLowerCase());
  110.             })
  111.         }
  112.  
  113.         for (const comment of comments) {
  114.             result.push(`-- ${comment.id}. ${comment.username}: ${comment.content}`);
  115.  
  116.             if (sortingType === 'asc') {
  117.                 comment.replies.sort((a, b) => {
  118.                     return a.id.localeCompare(b.id);
  119.                 });
  120.             } else if (sortingType === 'desc') {
  121.                 comment.replies.sort((a, b) => {
  122.                     return b.id.localeCompare(a.id);
  123.                 });
  124.             } else if (sortingType === 'username') {
  125.                 comment.replies.sort((a, b) => {
  126.                     return a.username.localeCompare(b.username);
  127.                 });
  128.             }
  129.  
  130.             for (const reply of comment.replies) {
  131.                 result.push(`--- ${reply.id}. ${reply.username}: ${reply.content}`);
  132.             }
  133.         }
  134.  
  135.         return result.join('\n');
  136.     };
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement