PPetkov2000

JS-Advanced Exams: Article

Jun 7th, 2020
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 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 = [];
  7. }
  8.  
  9. set likes(x) {
  10. this._likes = x;
  11. }
  12.  
  13. set comments(x) {
  14. this._comments = x;
  15. }
  16.  
  17. get likes() {
  18. let hasLikesArr = this._likes.filter(x => x.likes === 1);
  19.  
  20. if (hasLikesArr.length === 0) {
  21. return `${this.title} has 0 likes`;
  22. } else if (hasLikesArr.length === 1) {
  23. let result = "";
  24. this._likes.forEach(
  25. user => (result += `${user.username} likes this article!\n`)
  26. );
  27. return result.trim();
  28. } else {
  29. let firstLike = hasLikesArr[0].username;
  30. let totalLikes = hasLikesArr.length - 1;
  31. return `${firstLike} and ${totalLikes} others like this article!`;
  32. }
  33. }
  34.  
  35. like(username) {
  36. const user = this._likes.find(u => u.username === username);
  37.  
  38. if (user) {
  39. throw new Error("You can't like the same article twice!");
  40. }
  41.  
  42. if (this.creator === username) {
  43. throw new Error("You can't like your own articles!");
  44. }
  45.  
  46. this._likes.push({ username: username, likes: 1 });
  47.  
  48. return `${username} liked ${this.title}!`;
  49. }
  50.  
  51. dislike(username) {
  52. const user = this._likes.find(u => u.username === username);
  53.  
  54. if (!user) {
  55. throw new Error("You can't dislike this article!");
  56. }
  57.  
  58. user.likes--;
  59.  
  60. return `${username} disliked ${this.title}`;
  61. }
  62.  
  63. comment(username, content, id) {
  64. const comment = this._comments.find(c => c.id === id);
  65.  
  66. if (comment === undefined) {
  67. this._comments.push({
  68. id: this._comments.length + 1,
  69. username: username,
  70. content: content,
  71. replies: []
  72. });
  73. return `${username} commented on ${this.title}`;
  74. }
  75.  
  76. comment.replies.push({
  77. id: id + (comment.replies.length * 0.1 + 0.1),
  78. username: username,
  79. content: content
  80. });
  81.  
  82. return "You replied successfully";
  83. }
  84.  
  85. toString(sortingType) {
  86. let result = `Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${
  87. this._likes.filter(x => x.likes === 1).length
  88. }\nComments:\n`;
  89.  
  90. this._comments.sort(sortByCriteria).forEach(comment => {
  91. addToResult(comment, "comment");
  92. comment.replies
  93. .sort(sortByCriteria)
  94. .forEach(reply => addToResult(reply, "reply"));
  95. });
  96.  
  97. function sortByCriteria(a, b) {
  98. const map = {
  99. asc: () => a.id - b.id,
  100. desc: () => b.id - a.id,
  101. username: () => a.username.localeCompare(b.username)
  102. };
  103.  
  104. return map[sortingType]();
  105. }
  106.  
  107. function addToResult(typeObj, type) {
  108. const map = {
  109. comment: () =>
  110. (result += `-- ${typeObj.id}. ${typeObj.username}: ${typeObj.content}\n`),
  111. reply: () =>
  112. (result += `--- ${typeObj.id}. ${typeObj.username}: ${typeObj.content}\n`)
  113. };
  114.  
  115. return map[type]();
  116. }
  117.  
  118. return result.trim();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment