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: 2 additions & 3 deletions src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* 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 are rounded to nearest, so 36h ago reads "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
const diffDays = Math.round(Math.abs(diffHours) / 24)

if (Math.abs(diffSec) < 60) return "just now"
if (Math.abs(diffMin) < 60) {
Expand Down
31 changes: 26 additions & 5 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@ 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 to at most maxLength characters, cutting at a word
* boundary and appending an ellipsis. The ellipsis counts toward maxLength,
* so the result is never longer than maxLength.
*/
export function truncate(str: string, maxLength: number): string {
throw new Error("not implemented")
if (!str || str.length <= maxLength) return str

const ellipsis = "..."
if (maxLength <= ellipsis.length) return str.slice(0, Math.max(maxLength, 0))

// Leading whitespace would otherwise eat the character budget and could
// reduce the result to a bare ellipsis. Trim it here rather than at entry so
// a within-limit string is still returned byte-for-byte unchanged above.
const source = str.trimStart()
if (source.length <= maxLength) return source

const budget = maxLength - ellipsis.length
const candidate = source.slice(0, budget)
const lastSpace = candidate.lastIndexOf(" ")
// `source` starts with a non-whitespace character, so `candidate` does too:
// whichever branch is taken, the body keeps that character and survives
// trimEnd() non-empty. Content can therefore never be lost entirely.
const body = lastSpace > 0 ? candidate.slice(0, lastSpace) : candidate

return body.trimEnd() + ellipsis
}

export function slugify(str: string): string {
Expand All @@ -24,8 +46,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
}
42 changes: 34 additions & 8 deletions src/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,46 @@ 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 and/or priority of a task.
*
* Omitted keys are always left untouched. An explicit `undefined` behaves
* differently per field, because `title` and `priority` are required on Task
* while `description` is optional:
* - `title` / `priority`: an explicit `undefined` is ignored, so a required
* field can never be blanked out.
* - `description`: an explicit `undefined` clears it.
*
* Returns true if updated, false if the task was 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 ("title" in changes && changes.title !== undefined) task.title = changes.title
if ("description" in changes) task.description = changes.description
if ("priority" in changes && 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, without mutating internal order. */
sortBy(field: "priority" | "createdAt" | "status"): Task[] {
throw new Error("not implemented")
const priorityOrder: Record<Priority, number> = { high: 0, medium: 1, low: 2 }
const statusOrder: Record<Status, number> = { in_progress: 0, pending: 1, completed: 2 }

return Array.from(this.tasks.values()).sort((a, b) => {
switch (field) {
case "priority":
return priorityOrder[a.priority] - priorityOrder[b.priority]
case "status":
return statusOrder[a.status] - statusOrder[b.status]
case "createdAt":
return a.createdAt.getTime() - b.createdAt.getTime()
}
})
}
}
16 changes: 7 additions & 9 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

/**
* 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 TLDs of any length (.museum).
*/
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)
// Bound the input before matching so the regex cannot be driven into
// pathological backtracking (RFC 5321 caps a forward path at 254 chars).
if (!value || value.length > 254) return false
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 (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