Advertisement
Nina-S

Untitled

Feb 21st, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import { Task } from './board-items/task.model.js';
  2.  
  3. /** The Board class holds items */
  4. export class Board {
  5. #items;
  6.  
  7. constructor() {
  8. this.#items = [];
  9. }
  10.  
  11. get items() {
  12. return this.#items.slice();
  13. }
  14.  
  15. get count() {
  16. return this.#items.length;
  17. }
  18.  
  19. add(item) {
  20. if (!(item instanceof Task)) {
  21. throw new Error('The provided value is not an objected created from the BoardItem class!');
  22. }
  23.  
  24. const itemIndex = this.#items.findIndex(existingItem => existingItem === item);
  25.  
  26. if (itemIndex >= 0) {
  27. throw new Error('The provided item already exists in this board!');
  28. }
  29.  
  30. this.#items.push(item);
  31. }
  32.  
  33. remove(item) {
  34. const itemIndex = this.#items.findIndex(existingItem => existingItem === item);
  35.  
  36. if (itemIndex < 0) {
  37. throw new Error('The provided task does not exist in this board!');
  38. }
  39.  
  40. this.#items.splice(itemIndex, 1);
  41. }
  42.  
  43. /** Transforms the board data into a formatted string. */
  44. toString() {
  45. const titles = '---Items Board---\n\nItems:\n\n';
  46.  
  47. if (this.#items.length) {
  48. return titles + this.#items.join('\n--------\n');
  49. }
  50.  
  51. return `${titles}No items at the moment.`;
  52. }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement