| 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 |
131x
127x
4x
131x
104x
27x
131x
103x
2x
33x
2x
309x
200x
196x
196x
4x
4x
4x
16x
16x
2x
2x
2x
2x
2x
1x
1x
1x
34x
116x
116x
114x
116x
116x
48x
116x
7x
1x
1x
1x
1x
55x
55x
55x
2x
2x
2x
2x
1x
9x
9x
9x
18x
14x
18x
139x
84x
84x
82x
84x
9x
1x
19x
19x
19x
51x
26x
51x
129x
129x
106x
106x
183x
15x
201x
201x
201x
168x
33x
168x
168x
106x
62x
1x
| 'use strict';
/*
* PUBLIC API
*/
class Base {
constructor(title, desc) {
if (title) {
this._title = sanitizeTitle(title);
}
else {
this._title = '';
}
if (desc) {
this._desc = sanitizeDesc(desc);
}
else {
this._desc = '';
}
this._content = [];
}
get title() { return this._title; }
set title(newTitle) { this._title = sanitizeTitle(newTitle); }
get desc() { return this._desc; }
set desc(newDesc) { this._desc = sanitizeDesc(newDesc); }
get length() { return this._content.length; }
insert(obj, index) {
if (index >= this._content.length) {
this._content.push(obj);
return this._content.length - 1;
}
Eif (index <= 0) {
this._content.unshift(obj);
return 0;
}
this._content.splice(index, 0, obj);
return index;
}
remove(index) {
Iif (index < 0 || index > this._content.length) {
return null;
}
return this._content.splice(index, 1);
}
swap(index1, index2) {
Iif (index1 < 0 || index2 < 0 || index1 >= this.length || index2 >= this.length) {
return null;
}
const tempItem = this._content[index1];
this._content[index1] = this._content[index2];
this._content[index2] = tempItem;
return true;
}
}
class List extends Base {
get done() {
let done = 0;
this._content.forEach(item => {
if (item.done) {
++done;
}
});
return done;
}
getItem(index) { return this._content[index]; }
insertItem(value, state, index) {
value = String(value);
if (typeof index !== 'number' || isNaN(index)) {
index = this.length;
}
const insertedIndex = super.insert({value, done: false}, index);
if (state) {
this.markItem(insertedIndex, state);
}
return insertedIndex;
}
removeItem(index) {
return super.remove(index);
}
changeItem(index, value) {
Iif (index < 0 || index >= this.length) {
return null;
}
value = String(value);
this._content[index].value = value;
return value;
}
markItem(index, state) {
Iif (index < 0 || index >= this.length) {
return null;
}
this._content[index].done = state === true;
return true;
}
toggleItem(index) {
Iif (index < 0 || index >= this.length) {
return null;
}
const done = !this._content[index].done;
this._content[index].done = done;
return done;
}
swapItems(index1, index2) {
return super.swap(index1, index2);
}
findItems(search) {
const flags = /[A-Z]/.test(search) ? '' : 'i';
const re = new RegExp(search, flags);
return this._content.reduce((acc, cur, i) => {
if (re.test(cur.value)) {
acc.push(i);
}
return acc;
}, []);
}
}
class Project extends Base {
getList(index) { return this._content[index]; }
insertList(list, index) {
Iif (!(list instanceof List)) {
return null;
}
if (typeof index !== 'number' || isNaN(index)) {
index = this.length;
}
return super.insert(list, index);
}
removeList(index) {
return super.remove(index);
}
swapLists(index1, index2) {
return super.swap(index1, index2);
}
findLists(search, desc) {
const flags = /[A-Z]/.test(search) ? '' : 'i';
const re = new RegExp(search, flags);
return this._content.reduce((acc, cur, i) => {
if (re.test(cur.title) || (desc && re.test(cur.desc))) {
acc.push(i);
}
return acc;
}, []);
}
}
/*
* PRIVATE FUNCTIONS
*/
function sanitizeTitle(title) {
Iif (!title) {
return '';
}
return title.replace('\n', ' ').replace(/^\s+/, '');
}
function sanitizeDesc(desc) {
Iif (!desc) {
return '';
}
return desc.split('\n').reduce((acc, cur) => {
if (/^\s*$/.test(cur)) {
return acc;
}
function sanitize(string) {
let san = string.replace(/^\s*#+/, ''); // Strip leading #
san = san.replace(/^\s*[-*]+/, ''); // Strip leading - or *
if (san === string) {
return san; // Fully sanitized
}
return sanitize(san); // Sanitize again
}
cur = sanitize(cur);
if (acc.length === 0) {
return cur;
}
return acc.concat('\n' + cur);
}, '');
}
/*
* MODULE EXPORTS
*/
module.exports = {
List,
Project
};
|