Skip to content

Commit d435a6a

Browse files
author
QuantCode Agent
committed
fix: resolve 16 failing tests across calculator, string-utils, task-manager, date-utils, and validator
- calculator: throw error on division by zero instead of returning Infinity - string-utils: fix wordCount to handle multiple consecutive spaces - string-utils: implement truncate with word-boundary support - task-manager: implement remove, update, and sortBy methods - date-utils: use Math.round for day calculation in formatRelative - validator: allow TLDs longer than 4 chars in isEmail - validator: support port numbers in isUrl
1 parent 2354bc5 commit d435a6a

5 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/calculator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export function multiply(a: number, b: number): number {
1717

1818
// BUG: Division by zero is not handled
1919
export function divide(a: number, b: number): number {
20+
if (b === 0) throw new Error("Division by zero")
2021
return a / b
2122
}

src/date-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function formatRelative(date: Date, now: Date = new Date()): string {
1414
const diffSec = diffMs / 1000
1515
const diffMin = diffSec / 60
1616
const diffHours = diffMin / 60
17-
const diffDays = Math.floor(diffHours / 24) // BUG: should be Math.round
17+
const diffDays = Math.round(diffHours / 24)
1818

1919
if (Math.abs(diffSec) < 60) return "just now"
2020
if (Math.abs(diffMin) < 60) {

src/string-utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export function reverse(str: string): string {
1414
// TODO: implement truncate — should truncate at a word boundary, with "..."
1515
// counting toward maxLength. Return unchanged if str.length <= maxLength.
1616
export function truncate(str: string, maxLength: number): string {
17-
throw new Error("not implemented")
17+
if (str.length <= maxLength) return str
18+
const cutAt = maxLength - 3
19+
const spaceIndex = str.lastIndexOf(" ", cutAt)
20+
if (spaceIndex > 0) return str.slice(0, spaceIndex) + "..."
21+
return str.slice(0, cutAt) + "..."
1822
}
1923

2024
export function slugify(str: string): string {
@@ -27,5 +31,5 @@ export function slugify(str: string): string {
2731
// BUG: This doesn't handle multiple consecutive spaces
2832
export function wordCount(str: string): number {
2933
if (!str.trim()) return 0
30-
return str.split(" ").length
34+
return str.trim().split(/\s+/).filter(Boolean).length
3135
}

src/task-manager.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,27 @@ export class TaskManager {
5454

5555
// TODO: implement — remove a task by id, return true if removed, false if not found
5656
remove(id: string): boolean {
57-
throw new Error("not implemented")
57+
if (!this.tasks.has(id)) return false
58+
this.tasks.delete(id)
59+
return true
5860
}
5961

60-
// TODO: implement — update title/description/priority of a task
61-
// return true if updated, false if not found
6262
update(id: string, changes: Partial<Pick<Task, "title" | "description" | "priority">>): boolean {
63-
throw new Error("not implemented")
63+
const task = this.tasks.get(id)
64+
if (!task) return false
65+
if (changes.title !== undefined) task.title = changes.title
66+
if (changes.description !== undefined) task.description = changes.description
67+
if (changes.priority !== undefined) task.priority = changes.priority
68+
return true
6469
}
6570

66-
// TODO: implement — return all tasks sorted by the given field
67-
// priority sort order: high > medium > low
6871
sortBy(field: "priority" | "createdAt" | "status"): Task[] {
69-
throw new Error("not implemented")
72+
const priorityOrder: Record<Priority, number> = { high: 0, medium: 1, low: 2 }
73+
return Array.from(this.tasks.values()).sort((a, b) => {
74+
if (field === "priority") return priorityOrder[a.priority] - priorityOrder[b.priority]
75+
if (field === "createdAt") return a.createdAt.getTime() - b.createdAt.getTime()
76+
return a.status.localeCompare(b.status)
77+
})
7078
}
79+
7180
}

src/validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
export function isEmail(value: string): boolean {
1212
// BUG: too restrictive — missing subdomain support and long TLDs
13-
return /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,4}$/.test(value)
13+
return /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}$/.test(value)
1414
}
1515

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

0 commit comments

Comments
 (0)