From c90bf15e26a5a19080a4005d353e3198e3790082 Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Sun, 2 Aug 2026 16:28:31 +0000 Subject: [PATCH] fix: implement missing utilities and fix edge-case bugs to pass all tests Fixes 16 failing tests across the library: - calculator: divide now throws on division by zero - string-utils: wordCount collapses consecutive whitespace; implement truncate (word-boundary aware, ellipsis counts toward maxLength) - task-manager: implement remove, update (per-field guards), and sortBy (priority/status rank, createdAt oldest-first) - date-utils: fix off-by-one in formatRelative day rounding (36h -> 2 days) - validator: isEmail accepts multi-level subdomains and long TLDs; isUrl accepts hosts with explicit ports No test files modified; no new dependencies. --- src/calculator.ts | 2 +- src/date-utils.ts | 5 +---- src/string-utils.ts | 17 ++++++++++++----- src/task-manager.ts | 27 +++++++++++++++++++-------- src/validator.ts | 13 ++++--------- 5 files changed, 37 insertions(+), 27 deletions(-) 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..4aad361 100644 --- a/src/date-utils.ts +++ b/src/date-utils.ts @@ -5,16 +5,13 @@ /** * Format a date as a human-readable relative string. * e.g. "2 days ago", "just now", "in 3 hours" - * - * BUG: off-by-one — uses Math.floor where Math.round is needed for days, - * causing "1 day ago" to appear for anything from 12h to 47h. */ export function formatRelative(date: Date, now: Date = new Date()): string { const diffMs = now.getTime() - date.getTime() 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(Math.abs(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..cf3bd88 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -11,10 +11,18 @@ 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 ellipsis = "..." + const budget = maxLength - ellipsis.length + if (budget <= 0) return str.slice(0, Math.max(maxLength, 0)) + + const head = str.slice(0, budget) + const lastSpace = head.lastIndexOf(" ") + const body = lastSpace > 0 ? head.slice(0, lastSpace) : head + + return body.trimEnd() + ellipsis } export function slugify(str: string): string { @@ -24,8 +32,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..d56e220 100644 --- a/src/task-manager.ts +++ b/src/task-manager.ts @@ -52,20 +52,31 @@ 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>): boolean { - throw new Error("not implemented") + 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 } - // 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 priorityRank: Record = { high: 0, medium: 1, low: 2 } + const statusRank: Record = { in_progress: 0, pending: 1, completed: 2 } + const result = Array.from(this.tasks.values()) + + switch (field) { + case "priority": + return result.sort((a, b) => priorityRank[a.priority] - priorityRank[b.priority]) + case "status": + return result.sort((a, b) => statusRank[a.status] - statusRank[b.status]) + case "createdAt": + return result.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()) + } } } diff --git a/src/validator.ts b/src/validator.ts index 27bf385..3d01ddb 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -4,25 +4,20 @@ /** * Returns true if the string is a valid email address. - * - * BUG: the regex does not allow subdomains (e.g. user@mail.example.com fails) - * and rejects valid TLDs longer than 4 chars (e.g. .museum, .travel). + * Supports subdomains (user@mail.example.com) and long TLDs (.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@.]+(\.[^\s@.]+)*\.[a-zA-Z]{2,}$/.test(value) } /** * Returns true if the string is a valid URL (http or https). - * - * BUG: rejects URLs with ports (e.g. http://localhost:3000) + * Ports are permitted (e.g. http://localhost:3000). */ 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 }