Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ToDoItem {
- constructor (toDoItem, date, finalDate = undefined) {
- let _text = toDoItem;
- this.getText = function() {
- return _text;
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'text', {
- get: function() {
- return _text;
- },
- set: function(string) {
- if (typeof string !== 'string') {
- throw new TypeError('TypeError');
- } else if (!(string.length > 0 && string.length < 51)) {
- throw new Error('Error');
- }
- _text = string;
- },
- catch(e) {
- console.log(e);
- }
- });
- let _dueDate = new Date(date);
- this.getDueDate = function() {
- return _dueDate;
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'dueDate', {
- get: function() {
- return new Date(_dueDate);
- },
- set: function(getDate) {
- if (typeof getDate !== 'object' || getDate === null ||
- Array.isArray(getDate) === true || (Object.keys(getDate).length ===
- 0 && getDate.constructor === Object)) {
- throw new TypeError('TypeError');
- }
- _dueDate = new Date(getDate);
- },
- catch(e) {
- console.log(e);
- }
- });
- let _finishedDate = finalDate;
- if (typeof finalDate !== 'undefined') {
- _finishedDate = new Date(finalDate);
- }
- this.getFinishedDate = function() {
- return _finishedDate;
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'finishedDate', {
- get: function() {
- return new Date(_finishedDate);
- },
- set: function(date) {
- if (typeof date !== 'object' || (Object.keys(date).length ===
- 0 && date.constructor === Object) && typeof date !== 'undefined') {
- throw new Error('typeError');
- }
- if (typeof date === 'undefined') {
- _finishedDate = undefined;
- } else {
- _finishedDate = new Date(date);
- }
- if (_finishedDate < _dueDate) {
- _isDone = true;
- _isOverdue = false;
- } else {
- _isDone = false;
- _isOverdue = true;
- }
- }
- });
- let _isDone = false;
- //read me
- Object.defineProperty(this, 'isDone', {
- get: function() {
- return _isDone;
- }
- });
- let _isOverdue = false;
- this.getOverdue = function() {
- return _isOverdue;
- };
- //read me
- Object.defineProperty(this, 'isOverdue', {
- get: function() {
- return _isOverdue;
- }
- });
- }
- //tar privata värden från funktionen som kallar och gör en ny objekt
- clone() {
- return new ToDoItem(this.getText(), new Date(this.getDueDate()),
- new Date(this.getFinishedDate()));
- }
- toJson() {
- // ??
- }
- //tar privata värden och sätter in i retur, konverterar det till en sträng
- toString() {
- if (this.getOverdue() === true) {
- return String('* ' + this.getText() + ' ' + this.getDueDate());
- } else if (this.getOverdue() === false) {
- return String(' ' + this.getText() + ' ' + this.getDueDate());
- }
- }
- }
- ---------------------------------------------------------------------------------------------------
- class ToDoList {
- constructor (name, color, toDoItems) {
- let _name = name;
- this.getName = function() {
- return _name;
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'name', {
- get: function() {
- return _name;
- },
- set: function(string) {
- if (typeof string !== 'string') {
- throw new TypeError('TypeError');
- } else if (!(string.length > 0 && string.length < 31)) {
- throw new Error('Error');
- }
- _name = string;
- },
- catch(e) {
- console.log(e);
- }
- });
- let _color = color || 'yellow';
- this.getColor = function() {
- return _color;
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'color', {
- get: function() {
- return _color;
- },
- set: function(string) {
- if (typeof string !== 'string') {
- throw new TypeError('TypeError');
- } else if (!(string.length > 0 && string.length < 21)) {
- throw new Error('Error');
- }
- _color = string;
- },
- catch(e) {
- console.log(e);
- }
- });
- let _toDoItems = [];
- _toDoItems.push(toDoItems);
- this.doItems = function() {
- return this.specificClone(_toDoItems);
- };
- //kontrollerar värdet med get, set.
- Object.defineProperty(this, 'toDoItem', {
- get: function() {
- return this.specificClone(toDoItems);
- },
- set: function(obj) {
- if (typeof obj !== 'object') {
- throw new TypeError('TypeError');
- }
- _toDoItems.push(this.specificClone(toDoItems));
- },
- catch(e) {
- console.log(e);
- }
- });
- let _hasOverdue;
- //read me
- Object.defineProperty(this, 'hasOverdue', {
- get: function() {
- return _hasOverdue;
- }
- });
- }
- specificClone(item) {
- let newClone = [];
- console.log(item);
- for (let turns = 0; turns < item.length; turns++) {
- var obj = item[turns].clone();
- newClone.push(obj);
- }
- return newClone;
- }
- //försökte lägga till privata värdet med variabeln från parametern, misslyckades lösas
- add(toDoItem) {
- this.toDoItem = toDoItem;
- }
- removeFinished() {
- // ??
- }
- //tar privata värden från funktionen som kallar och gör en ny objekt
- clone() {
- let newName = this.getName();
- let newColor = this.getColor();
- let newItems = this.doItems();
- return new ToDoList(newName, newColor, newItems);
- }
- toJson() {
- // ??
- }
- toString() {
- let index = this.doItems();
- for (let turns = 0; turns < index.length; turns++) {
- if (typeof index[turns].getFinishedDate() !== 'undefined') {
- return String(index[turns].getText() + ' ' + index[turns].getDueDate() +
- ' ' + index[turns].getFinishedDate());
- } else {
- return String(index[turns].getText() + ' ' + index[turns].getDueDate());
- }
- }
- }
- }
- /**
- * Exports.
- */
- module.exports = ToDoList;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement