Skip to content

Commit

Permalink
feat: TodoList.getSortedList 구현 resolve #97
Browse files Browse the repository at this point in the history
  • Loading branch information
n-ryu committed Nov 28, 2022
1 parent bc86621 commit c2882f4
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions client/src/core/todo/todoList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ const DAY = 24 * 60 * 60 * 1000;

const onlyDate = (date: Date): Date => new Date(date.getFullYear(), date.getMonth(), date.getDate());
const isEqualDate = (d1: Date, d2: Date): boolean => onlyDate(d1).getTime() === onlyDate(d2).getTime();

const compareFunctions = {
ascendImminence: (a: Todo, b: Todo): number => {
const newToday = new Date();
return -Number(isEqualDate(newToday, a.until)) + Number(isEqualDate(newToday, b.until));
},
descendImminence: (a: Todo, b: Todo): number => {
const newToday = new Date();
return Number(isEqualDate(newToday, a.until)) - Number(isEqualDate(newToday, b.until));
},
ascendDeadline: (a: Todo, b: Todo): number => a.until.getTime() - b.until.getTime(),
descendDeadline: (a: Todo, b: Todo): number => -a.until.getTime() + b.until.getTime(),
ascendImportance: (a: Todo, b: Todo): number => -b.importance + a.importance,
descendImportance: (a: Todo, b: Todo): number => b.importance - a.importance,
ascendLastPostponed: (a: Todo, b: Todo): number => a.lastPostponed.getTime() - b.lastPostponed.getTime(),
descendLastPostponed: (a: Todo, b: Todo): number => -a.lastPostponed.getTime() + b.lastPostponed.getTime(),
ascendTitle: (a: Todo, b: Todo): number => {
if (a.title === b.title) return 0;
if (a.title < b.title) return -1;
return 1;
},
descendTitle: (a: Todo, b: Todo): number => {
if (a.title === b.title) return 0;
if (a.title < b.title) return 1;
return -1;
},
};

export interface InputTodo {
id: string; // UUIDv4, 할일의 고유 id
title: string; // VARCHAR(255), 할일의 이름
Expand Down Expand Up @@ -130,7 +158,6 @@ export class Todo implements InputTodo {
};
}
}

export class TodoList {
private readonly todoList: Todo[];
constructor(todoList: InputTodo[]) {
Expand Down Expand Up @@ -227,7 +254,18 @@ export class TodoList {
return new TodoList(this.todoList.filter((el) => el.id !== id));
}

async getSortedList(type: string, compareArr: string[]): Promise<TodoList> {
async getSortedList(type: 'READY' | 'WAIT' | 'DONE', compareArr: string[]): Promise<TodoList> {
const generateCompare = (compareArr: string[]) => {
return (a: Todo, b: Todo): number => {
let result = 0;
for (let i = 0; i < compareArr.length; i++) {
result = compareFunctions[compareArr[i] as keyof typeof compareFunctions](a, b);
if (result !== 0) break;
}
return result;
};
};
this.todoList.filter((el) => el.state === type).sort(generateCompare(compareArr));
return new TodoList(this.todoList);
}
}

0 comments on commit c2882f4

Please sign in to comment.