-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-manager.ts
More file actions
83 lines (71 loc) · 2.39 KB
/
Copy pathtask-manager.ts
File metadata and controls
83 lines (71 loc) · 2.39 KB
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
/**
* Simple task manager.
* Manages a list of tasks with priorities and status tracking.
*/
export type Priority = "low" | "medium" | "high"
export type Status = "pending" | "in_progress" | "completed"
export interface Task {
id: string
title: string
description?: string
priority: Priority
status: Status
createdAt: Date
completedAt?: Date
}
const PRIORITY_ORDER: Record<Priority, number> = { low: 0, medium: 1, high: 2 }
const STATUS_ORDER: Record<Status, number> = { pending: 0, in_progress: 1, completed: 2 }
export class TaskManager {
private tasks: Map<string, Task> = new Map()
private nextId = 1
add(title: string, priority: Priority = "medium", description?: string): Task {
const task: Task = {
id: String(this.nextId++),
title,
description,
priority,
status: "pending",
createdAt: new Date(),
}
this.tasks.set(task.id, task)
return task
}
get(id: string): Task | undefined {
return this.tasks.get(id)
}
list(filter?: { status?: Status; priority?: Priority }): Task[] {
let result = Array.from(this.tasks.values())
if (filter?.status) result = result.filter((t) => t.status === filter.status)
if (filter?.priority) result = result.filter((t) => t.priority === filter.priority)
return result
}
complete(id: string): boolean {
const task = this.tasks.get(id)
if (!task) return false
task.status = "completed"
task.completedAt = new Date()
return true
}
remove(id: string): boolean {
return this.tasks.delete(id)
}
update(id: string, changes: Partial<Pick<Task, "title" | "description" | "priority">>): boolean {
const task = this.tasks.get(id)
if (!task) return false
if (changes.title !== undefined) task.title = changes.title
if (changes.description !== undefined) task.description = changes.description
if (changes.priority !== undefined) task.priority = changes.priority
return true
}
sortBy(field: "priority" | "createdAt" | "status"): Task[] {
const tasks = Array.from(this.tasks.values())
switch (field) {
case "priority":
return tasks.sort((a, b) => PRIORITY_ORDER[b.priority] - PRIORITY_ORDER[a.priority])
case "createdAt":
return tasks.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
case "status":
return tasks.sort((a, b) => STATUS_ORDER[a.status] - STATUS_ORDER[b.status])
}
}
}