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
2 changes: 1 addition & 1 deletion src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 19 additions & 6 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ 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 "...".
* The ellipsis counts toward maxLength. Returns the string unchanged
* if it already fits within 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 ellipsis.slice(0, Math.max(maxLength, 0))

let sliced = str.slice(0, budget)
const lastSpace = sliced.lastIndexOf(" ")
if (lastSpace > 0) sliced = sliced.slice(0, lastSpace)

return sliced.trimEnd() + ellipsis
}

export function slugify(str: string): string {
Expand All @@ -24,8 +37,8 @@ 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
const trimmed = str.trim()
if (!trimmed) return 0
return trimmed.split(/\s+/).length
}
39 changes: 31 additions & 8 deletions src/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,43 @@ export class TaskManager {
return true
}

// TODO: implement — remove a task by id, return true if removed, false if not found
/** Remove a task by id. Returns 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 the title, description or priority of a task.
* Returns 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
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
/**
* Return all tasks sorted by the given field.
* Priority orders high > medium > low; createdAt and status order ascending.
* The sort is stable, so equal keys retain insertion order.
*/
sortBy(field: "priority" | "createdAt" | "status"): Task[] {
throw new Error("not implemented")
const priorityRank: Record<Priority, number> = { high: 0, medium: 1, low: 2 }
const statusRank: Record<Status, number> = { pending: 0, in_progress: 1, completed: 2 }
const tasks = Array.from(this.tasks.values())

return tasks.sort((a, b) => {
switch (field) {
case "priority":
return priorityRank[a.priority] - priorityRank[b.priority]
case "status":
return statusRank[a.status] - statusRank[b.status]
case "createdAt":
return a.createdAt.getTime() - b.createdAt.getTime()
}
})
}
}
21 changes: 12 additions & 9 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
/**
* 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).
* The local part is matched permissively (dot-separated segments of any
* character except whitespace, "@" and control characters) so internationalised
* addresses such as josé@example.com are accepted. The domain is matched
* strictly: dot-separated LDH labels and a 2–63 character alphabetic TLD,
* which supports subdomains 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)
if (value.length > 254) return false
const localPart = String.raw`[^\s@.\x00-\x1F\x7F]+(?:\.[^\s@.\x00-\x1F\x7F]+)*`
const domain = String.raw`(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,63}`
return new RegExp(`^${localPart}@${domain}$`, "u").test(value)
}

/**
* Returns true if the string is a valid URL (http or https).
*
* BUG: rejects URLs with ports (e.g. http://localhost:3000)
* Returns true if the string is a valid URL using the http or https scheme.
* 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
}
Expand Down
Loading