Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export function multiply(a: number, b: number): number {

// BUG: Division by zero is not handled
export function divide(a: number, b: number): number {
if (b === 0) throw new Error("Division by zero")
return a / b
}
2 changes: 1 addition & 1 deletion src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function formatRelative(date: Date, now: Date = new Date()): string {
const diffSec = diffMs / 1000
const diffMin = diffSec / 60
const diffHours = diffMin / 60
const diffDays = Math.floor(diffHours / 24) // BUG: should be Math.round
const diffDays = Math.round(diffHours / 24)

if (Math.abs(diffSec) < 60) return "just now"
if (Math.abs(diffMin) < 60) {
Expand Down
15 changes: 13 additions & 2 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ export function reverse(str: string): string {
// TODO: implement truncate — should truncate at a word boundary, with "..."
// counting toward maxLength. Return unchanged if str.length <= maxLength.
export function truncate(str: string, maxLength: number): string {
throw new Error("not implemented")
if (str.length <= maxLength) return str
const words = str.split(" ")
let result = ""
for (const word of words) {
const candidate = result ? result + " " + word : word
if ((candidate + "...").length <= maxLength) {
result = candidate
} else {
break
}
}
return result + "..."
}

export function slugify(str: string): string {
Expand All @@ -27,5 +38,5 @@ export function slugify(str: string): string {
// BUG: This doesn't handle multiple consecutive spaces
export function wordCount(str: string): number {
if (!str.trim()) return 0
return str.split(" ").length
return str.trim().split(/\s+/).filter(Boolean).length
}
23 changes: 15 additions & 8 deletions src/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,27 @@ export class TaskManager {
return true
}

// TODO: implement — remove a task by id, return true if removed, false if not found
remove(id: string): boolean {
throw new Error("not implemented")
return this.tasks.delete(id)
}

// TODO: implement — update title/description/priority of a task
// return true if updated, false if not found
update(id: string, changes: Partial<Pick<Task, "title" | "description" | "priority">>): boolean {
throw new Error("not implemented")
const task = this.tasks.get(id)
if (!task) return false
Object.assign(task, changes)
return true
}

// TODO: implement — return all tasks sorted by the given field
// priority sort order: high > medium > low
sortBy(field: "priority" | "createdAt" | "status"): Task[] {
throw new Error("not implemented")
const tasks = Array.from(this.tasks.values())
if (field === "priority") {
const order: Record<Priority, number> = { high: 0, medium: 1, low: 2 }
return tasks.slice().sort((a, b) => order[a.priority] - order[b.priority])
}
if (field === "createdAt") {
return tasks.slice().sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
}
return tasks.slice().sort((a, b) => a.status.localeCompare(b.status))
}

}
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
export function isEmail(value: string): boolean {
// BUG: too restrictive — missing subdomain support and long TLDs
return /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,4}$/.test(value)
return /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}$/.test(value)
}

/**
Expand All @@ -22,7 +22,7 @@ export function isUrl(value: string): boolean {
try {
const url = new URL(value)
// BUG: only allows http/https but also rejects valid port usage
return (url.protocol === "http:" || url.protocol === "https:") && url.port === ""
return url.protocol === "http:" || url.protocol === "https:"
} catch {
return false
}
Expand Down