diff --git a/src/calculator.ts b/src/calculator.ts index 68b894d..8fa3de6 100644 --- a/src/calculator.ts +++ b/src/calculator.ts @@ -15,7 +15,7 @@ export function multiply(a: number, b: number): number { return a * b } -// 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 } diff --git a/src/date-utils.ts b/src/date-utils.ts index 37272a7..35c07de 100644 --- a/src/date-utils.ts +++ b/src/date-utils.ts @@ -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) { diff --git a/src/string-utils.ts b/src/string-utils.ts index 63fba18..0009ece 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -11,10 +11,14 @@ export function reverse(str: string): string { return str.split("").reverse().join("") } -// 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 budget = maxLength - 3 // reserve space for "..." + if (budget <= 0) return str.slice(0, maxLength) + const truncated = str.slice(0, budget) + const lastSpace = truncated.lastIndexOf(" ") + const cut = lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated + return cut + "..." } export function slugify(str: string): string { @@ -24,8 +28,7 @@ export function slugify(str: string): string { .replace(/^-|-$/g, "") } -// 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+/).length } diff --git a/src/task-manager.ts b/src/task-manager.ts index a920e85..b2e4bcc 100644 --- a/src/task-manager.ts +++ b/src/task-manager.ts @@ -52,20 +52,29 @@ 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") + if (!this.tasks.has(id)) return false + this.tasks.delete(id) + return true } - // TODO: implement — update title/description/priority of a task - // return true if updated, false if not found update(id: string, changes: Partial>): 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 = { high: 0, medium: 1, low: 2 } + return tasks.sort((a, b) => order[a.priority] - order[b.priority]) + } + if (field === "createdAt") { + return tasks.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()) + } + // status: alphabetical + return tasks.sort((a, b) => a.status.localeCompare(b.status)) } } diff --git a/src/validator.ts b/src/validator.ts index 27bf385..93736ac 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -9,8 +9,7 @@ * and rejects valid TLDs longer than 4 chars (e.g. .museum, .travel). */ 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) } /** @@ -21,8 +20,7 @@ export function isEmail(value: string): boolean { 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 }