From 44fe8c9c9052d8b5076134fdbb87d380d87edcbd Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Tue, 25 Aug 2026 16:27:41 +0000 Subject: [PATCH] fix: implement missing utilities and correct edge-case bugs Adds divide-by-zero guard, truncate and TaskManager remove/update/sortBy implementations, and fixes wordCount whitespace handling, relative-date day rounding, and email/URL validation bounds. --- src/calculator.ts | 2 +- src/date-utils.ts | 6 ++---- src/string-utils.ts | 20 +++++++++++++++----- src/task-manager.ts | 29 +++++++++++++++++++++-------- src/validator.ts | 12 +++++------- 5 files changed, 44 insertions(+), 25 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..d90a09b 100644 --- a/src/date-utils.ts +++ b/src/date-utils.ts @@ -6,15 +6,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. + * Day counts round to nearest, so 36 hours reads as "2 days ago". */ 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 if (Math.abs(diffSec) < 60) return "just now" if (Math.abs(diffMin) < 60) { @@ -25,7 +23,7 @@ export function formatRelative(date: Date, now: Date = new Date()): string { const h = Math.round(Math.abs(diffHours)) return diffMs > 0 ? `${h} hour${h !== 1 ? "s" : ""} ago` : `in ${h} hour${h !== 1 ? "s" : ""}` } - const d = Math.abs(diffDays) + const d = Math.round(Math.abs(diffHours) / 24) return diffMs > 0 ? `${d} day${d !== 1 ? "s" : ""} ago` : `in ${d} day${d !== 1 ? "s" : ""}` } diff --git a/src/string-utils.ts b/src/string-utils.ts index 63fba18..d876f70 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -11,10 +11,21 @@ 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. +/** + * Truncate a string at a word boundary, appending "..." which counts toward + * maxLength. Returns the string unchanged if it already fits. + */ export function truncate(str: string, maxLength: number): string { - throw new Error("not implemented") + if (str.length <= maxLength) return str + + const ellipsis = "..." + if (maxLength <= ellipsis.length) return str.slice(0, maxLength) + + let cut = str.slice(0, maxLength - ellipsis.length) + const lastSpace = cut.lastIndexOf(" ") + if (lastSpace > 0) cut = cut.slice(0, lastSpace) + + return cut.trimEnd() + ellipsis } export function slugify(str: string): string { @@ -24,8 +35,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..543bf91 100644 --- a/src/task-manager.ts +++ b/src/task-manager.ts @@ -16,6 +16,9 @@ export interface Task { completedAt?: Date } +const PRIORITY_RANK: Record = { high: 0, medium: 1, low: 2 } +const STATUS_RANK: Record = { pending: 0, in_progress: 1, completed: 2 } + export class TaskManager { private tasks: Map = new Map() private nextId = 1 @@ -52,20 +55,30 @@ 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 result = this.list() + switch (field) { + case "priority": + return result.sort( + (a, b) => PRIORITY_RANK[a.priority] - PRIORITY_RANK[b.priority] || Number(a.id) - Number(b.id), + ) + case "status": + return result.sort((a, b) => STATUS_RANK[a.status] - STATUS_RANK[b.status] || Number(a.id) - Number(b.id)) + case "createdAt": + return result.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || Number(a.id) - Number(b.id)) + } } } diff --git a/src/validator.ts b/src/validator.ts index 27bf385..1b3410a 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -5,24 +5,22 @@ /** * 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). + * This is a format check only, not a sanitiser — callers still need to encode + * or escape the value before rendering or storing it. */ 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) } /** * 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. This is a format check only, not a sanitiser. */ 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 }