krasizorbov

Article

Sep 21st, 2020 (edited)
822
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Article {
  2.   constructor(title, creator) {
  3.     this.title = title;
  4.     this.creator = creator;
  5.     this._comments = {};
  6.     this._likes = 0;
  7.     this.users = [];
  8.     this.commentsId = 0;
  9.     this.repliesId = 0;
  10.   }
  11.  
  12.   get likes() {
  13.     if (this._likes === 0) {
  14.       return `${this.title} has 0 likes`;
  15.     } else if (this._likes === 1) {
  16.       return `${this.users[0]} likes this article!`;
  17.     } else {
  18.       return `${this.users[0]} and ${this._likes} others like this article!`;
  19.     }
  20.   }
  21.  
  22.   like(username) {
  23.     if (this.users.includes(username)) {
  24.       throw new Error(`You can't like the same article twice!`);
  25.    } else if (this.creator === username) {
  26.      throw new Error(`You can't like your own articles!`);
  27.     } else {
  28.       this.users.push(username);
  29.       this._likes += 1;
  30.       return `${username} liked ${this.title}!`;
  31.     }
  32.   }
  33.  
  34.   dislike(username) {
  35.     if (!this.users.includes(username)) {
  36.       throw new Error(`You can't dislike this article!`);
  37.    } else {
  38.      this._likes -= 1;
  39.      let index = this.users.indexOf(username);
  40.      this.users.splice(index, 1);
  41.      return `${username} disliked ${this.title}`;
  42.    }
  43.  }
  44.  
  45.  comment(username, content, id) {
  46.    if (!this._comments.hasOwnProperty(id) || id === undefined) {
  47.      this._comments[++this.commentsId] = {
  48.        username: username,
  49.        content: content,
  50.        replies: [],
  51.      };
  52.      return `${username} commented on ${this.title}`;
  53.    } else {
  54.      this._comments[id].replies.push({
  55.        id: `${id}.${++this.repliesId}`,
  56.        username: username,
  57.        content: content,
  58.      });
  59.      return `You replied successfully`;
  60.    }
  61.  }
  62.  
  63.  toString(sortingType) {
  64.    let result = [
  65.      `Title: ${this.title}`,
  66.      `Creator: ${this.creator}`,
  67.      `Likes: ${this._likes}`,
  68.      `Comments:`,
  69.    ];
  70.    let sortedComments = null;
  71.    if (sortingType === "asc") {
  72.      sortedComments = Object.entries(this._comments).sort(
  73.        (a, b) => a[0] - b[0]
  74.      );
  75.    } else if (sortingType === "desc") {
  76.      sortedComments = Object.entries(this._comments).sort(
  77.        (a, b) => b[0] - a[0]
  78.      );
  79.    } else {
  80.      sortedComments = Object.entries(this._comments).sort((a, b) =>
  81.        a[1].username.localeCompare(b[1].username)
  82.      );
  83.    }
  84.  
  85.    for (const [key, value] of sortedComments) {
  86.      result.push(`-- ${key}. ${value.username}: ${value.content}`);
  87.      if (value.replies.length > 0) {
  88.        if (sortingType === "asc") {
  89.          let sorted = value.replies.sort((a, b) => a.id - b.id);
  90.          for (const s of sorted) {
  91.            result.push(`--- ${s.id}. ${s.username}: ${s.content}`);
  92.          }
  93.        } else if (sortingType === "desc") {
  94.          let sorted = value.replies.sort((a, b) => b.id - a.id);
  95.          for (const s of sorted) {
  96.            result.push(`--- ${s.id}. ${s.username}: ${s.content}`);
  97.          }
  98.        } else {
  99.          let sorted = value.replies.sort((a, b) =>
  100.            a.username.localeCompare(b.username)
  101.          );
  102.          for (const s of sorted) {
  103.            result.push(`--- ${s.id}. ${s.username}: ${s.content}`);
  104.          }
  105.        }
  106.      }
  107.    }
  108.    return result.join("\n");
  109.  }
  110. }
RAW Paste Data