|
| 1 | +/** |
| 2 | + * Simple task manager. |
| 3 | + * Manages a list of tasks with priorities and status tracking. |
| 4 | + */ |
| 5 | + |
| 6 | +export type Priority = "low" | "medium" | "high" |
| 7 | +export type Status = "pending" | "in_progress" | "completed" |
| 8 | + |
| 9 | +export interface Task { |
| 10 | + id: string |
| 11 | + title: string |
| 12 | + description?: string |
| 13 | + priority: Priority |
| 14 | + status: Status |
| 15 | + createdAt: Date |
| 16 | + completedAt?: Date |
| 17 | +} |
| 18 | + |
| 19 | +export class TaskManager { |
| 20 | + private tasks: Map<string, Task> = new Map() |
| 21 | + private nextId = 1 |
| 22 | + |
| 23 | + add(title: string, priority: Priority = "medium", description?: string): Task { |
| 24 | + const task: Task = { |
| 25 | + id: String(this.nextId++), |
| 26 | + title, |
| 27 | + description, |
| 28 | + priority, |
| 29 | + status: "pending", |
| 30 | + createdAt: new Date(), |
| 31 | + } |
| 32 | + this.tasks.set(task.id, task) |
| 33 | + return task |
| 34 | + } |
| 35 | + |
| 36 | + get(id: string): Task | undefined { |
| 37 | + return this.tasks.get(id) |
| 38 | + } |
| 39 | + |
| 40 | + list(filter?: { status?: Status; priority?: Priority }): Task[] { |
| 41 | + let result = Array.from(this.tasks.values()) |
| 42 | + if (filter?.status) result = result.filter((t) => t.status === filter.status) |
| 43 | + if (filter?.priority) result = result.filter((t) => t.priority === filter.priority) |
| 44 | + return result |
| 45 | + } |
| 46 | + |
| 47 | + complete(id: string): boolean { |
| 48 | + const task = this.tasks.get(id) |
| 49 | + if (!task) return false |
| 50 | + task.status = "completed" |
| 51 | + task.completedAt = new Date() |
| 52 | + return true |
| 53 | + } |
| 54 | + |
| 55 | + // TODO: implement remove method |
| 56 | + // TODO: implement update method to change title/description/priority |
| 57 | + // TODO: implement sortBy method (by priority, createdAt, or status) |
| 58 | +} |
0 commit comments