/
todo.js
111 lines (91 loc) · 1.58 KB
/
todo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* External dependencies.
*/
var assert = require('assert');
/**
* Todo item.
*
* @param {Number} id
* @param {String} description
* @param {String} status (optional)
* @param {String} modified (optional)
* @constructor
*/
function Todo(id, desc, status, modified) {
assert(id, 'please enter an id');
assert(desc, 'please enter a todo item');
this.id = +id;
this.desc = desc;
this.status = status || 'pending';
this.modified = modified || new Date;
}
/**
* Iterate over `arr` and construct a todo item
* for each element in it.
*
* @param {Array}
* @returns {Array}
* @api public
*/
Todo.create = function(arr) {
return arr.map(function(attrs) {
return new Todo(attrs.id, attrs.desc, attrs.status, attrs.modified);
});
};
/**
* Prototype.
*/
Todo.prototype = {
/**
* ID getter.
*
* @returns {Number}
*/
get id() {
return this._id;
},
/**
* ID setter.
*
* @param {Number} id
* @api public
*/
set id(id) {
this._id = +id;
assert(!Number.isNaN(this._id), 'ID must be a number');
},
/**
* Change the todo status to "pending".
*
* @api public
*/
undo: function() {
this.status = 'pending';
},
/**
* Change the todo status to "done".
*
* @api public
*/
complete: function() {
this.status = 'done';
},
/**
* toJSON.
*
* @returns {Object}
* @api public
*/
toJSON: function() {
return {
id: this.id,
desc: this.desc,
status: this.status,
modified: this.modified,
};
},
};
/**
* Primary export.
*/
module.exports = Todo;