Ivankooo1

Article

Oct 23rd, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. class Article {
  2. constructor(title, creator) {
  3. this.title = title;
  4. this.creator = creator;
  5. this._comments = [];
  6. this._likes = { likes: [], dislikes: [] };
  7. }
  8.  
  9. get likes() {
  10. if (this._likes.likes.length == 0) {
  11. return `${this.title} has 0 likes`;
  12. }
  13. if (this._likes.likes.length == 1) {
  14. return `${this._likes.likes[0]} likes this article!`;
  15. }
  16.  
  17. return `${this._likes.likes[0]} and ${this._likes.likes.length - 1} others like this article!`
  18. }
  19.  
  20. like(username) {
  21.  
  22. if (this._likes.likes.includes(username)) {
  23. throw new Error(`You can't like the same article twice!`);
  24. }
  25.  
  26. if (this.creator === username) {
  27. throw new Error(`You can't like your own articles!`);
  28. }
  29. this._likes.likes.push(username);
  30. return `${username} liked ${this.title}!`;
  31. }
  32.  
  33. dislike(username) {
  34. if (!this._likes.likes.includes(username)) {
  35. throw new Error('You can\'t dislike this article!');
  36. }
  37. let index = this._likes.likes.indexOf(username);
  38. this._likes.likes.splice(index, 1);
  39. this._likes.dislikes.push(username);
  40. return `${username} disliked ${this.title}`;
  41. }
  42.  
  43.  
  44. comment(username, content, id) {
  45. let checker = this._comments.find(x => x.Id == id);
  46. if (!checker || id == undefined) {
  47. let commentsAndReplies = {
  48. Id: this._comments.length + 1,
  49. Username: username,
  50. Content: content,
  51. Replies: []
  52. };
  53.  
  54. this._comments.push(commentsAndReplies);
  55. return `${username} commented on ${this.title}`;
  56. } else {
  57. checker.Replies.push({
  58. Id: `${checker.Id}.${checker.Replies.length + 1}`,
  59. Username: username,
  60. Content: content,
  61. });
  62. return `You replied successfully`;
  63. }
  64. }
  65.  
  66. toString(sortingType) {
  67. let sorted = this.sort(sortingType, this._comments);
  68.  
  69. let output = `Title: ${this.title}\n`;
  70. output += `Creator: ${this.creator}\n`;
  71. output += `Likes: ${this._likes.likes.length}\n`;
  72. output += 'Comments:\n';
  73.  
  74. sorted.forEach(x => {
  75. output += `-- ${x.Id}. ${x.Username}: ${x.Content}\n`;
  76. if (x.Replies.length !== 0) {
  77. let sortedReplies = this.sort(sortingType, x.Replies);
  78. sortedReplies.forEach(x => {
  79. output += `--- ${x.Id}. ${x.Username}: ${x.Content}\n`;
  80. })
  81. }
  82. });
  83.  
  84.  
  85. return output.trim();
  86. }
  87.  
  88.  
  89. sort(sortingType, target) {
  90. let sorted;
  91. if (sortingType === 'asc') {
  92. sorted = target.sort((a, b) => {
  93. let A = Object.values(a)[0];
  94. let B = Object.values(b)[0];
  95. return A - B;
  96. });
  97. } else if (sortingType === 'desc') {
  98. sorted = target.sort((a, b) => {
  99. let A = Object.values(a)[0];
  100. let B = Object.values(b)[0];
  101. return B - A;
  102. });
  103. } else if (sortingType === 'username') {
  104. sorted = target.sort((a, b) => {
  105. let A = Object.values(a)[1];
  106. let B = Object.values(b)[1];
  107. return A.localeCompare(B);
  108. });
  109. }
  110. return sorted;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment