todolists
Version:
Todolist in NodeJs CLI
134 lines • 3.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const todo_status_1 = require("./enum/todo_status");
class TodoList {
constructor(list) {
this.list = list;
}
get getList() { return this.list; }
get maxIdItem() {
if (this.list.length === 0) {
return { id: 0 };
}
return this.list.reduce((pre, cur) => {
if (pre.id > cur.id)
return pre;
return cur;
});
}
/**
* create item and insert todolist
* @param content
* @param deadline
* @param group
*/
create(content, deadline, group) {
const id = this.maxIdItem.id + 1;
const status = todo_status_1.todoStatusEnum.PENDING.name;
const todoItem = { id, content, status, deadline, group };
this.list.push(todoItem);
}
/**
* check the todo item as done
*
* @param {number} id
*
* @memberOf TodoList
*/
check(id) {
this.list.map(item => {
if (item.id === id) {
item.status = todo_status_1.todoStatusEnum.DONE.name;
}
});
}
/**
* Uncheck the todo item as pending
*
* @param {number} id
*
* @memberOf TodoList
*/
uncheck(id) {
this.list.map(item => {
if (item.id === id) {
item.status = todo_status_1.todoStatusEnum.PENDING.name;
}
});
}
/**
* get todo item by status
*
* @param {TodoStatusEnumItem} status
* @returns {Array<TodoItem>}
*
* @memberOf TodoList
*/
getItemListByStatus(status) {
return this.list.filter(item => {
return status.eql(item.status);
});
}
/**
* resort todo list id
*
* @returns {Array<TodoItem>}
*
* @memberOf TodoList
*/
resort() {
let start = 1;
const compare = function (pre, next) {
if (todo_status_1.todoStatusEnum.DONE.eql(pre.status) && todo_status_1.todoStatusEnum.PENDING.eql(next.status)) {
return 1;
}
if (pre.id > next.id) {
return 1;
}
return -1;
};
this.list = this.list.sort(compare).map(item => {
item.id = start;
start++;
return item;
});
return this.list;
}
/**
* remove all item
*
* @memberOf TodoList
*/
clearAll() {
this.list = [];
}
/**
* remove item by status
*
* @param {TodoStatusEnumItem} status
*
* @memberOf TodoList
*/
clearListByStatus(status) {
this.list = this.list.filter(item => !status.eql(item.status));
}
/**
* remove item by id
* @param id
*/
clearById(id) {
this.list = this.list.filter(item => item.id !== id);
}
/**
* stringify list to save into DB
*
* @returns {string}
*
* @memberOf TodoList
*/
toStringify() {
return JSON.stringify(this.list);
}
}
exports.default = TodoList;
//# sourceMappingURL=todo_list.js.map