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
4 changes: 3 additions & 1 deletion src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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
}
52 changes: 23 additions & 29 deletions src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,39 @@
* Date utility functions.
*/

/**
* 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 absDiffMs = Math.abs(diffMs)
const future = diffMs < 0

if (Math.abs(diffSec) < 60) return "just now"
if (Math.abs(diffMin) < 60) {
const m = Math.round(Math.abs(diffMin))
return diffMs > 0 ? `${m} minute${m !== 1 ? "s" : ""} ago` : `in ${m} minute${m !== 1 ? "s" : ""}`
}
if (Math.abs(diffHours) < 24) {
const h = Math.round(Math.abs(diffHours))
return diffMs > 0 ? `${h} hour${h !== 1 ? "s" : ""} ago` : `in ${h} hour${h !== 1 ? "s" : ""}`
const diffSeconds = Math.round(absDiffMs / 1000)
const diffMinutes = Math.round(absDiffMs / (1000 * 60))
const diffHours = Math.round(absDiffMs / (1000 * 60 * 60))
const diffDays = Math.round(absDiffMs / (1000 * 60 * 60 * 24))

if (diffSeconds < 60) return "just now"

if (future) {
if (diffMinutes < 60) return `in ${diffMinutes} minute${diffMinutes !== 1 ? "s" : ""}`
if (diffHours < 24) return `in ${diffHours} hour${diffHours !== 1 ? "s" : ""}`
return `in ${diffDays} day${diffDays !== 1 ? "s" : ""}`
}
const d = Math.abs(diffDays)
return diffMs > 0 ? `${d} day${d !== 1 ? "s" : ""} ago` : `in ${d} day${d !== 1 ? "s" : ""}`

if (diffMinutes < 60) return `${diffMinutes} minute${diffMinutes !== 1 ? "s" : ""} ago`
if (diffHours < 24) return `${diffHours} hour${diffHours !== 1 ? "s" : ""} ago`
return `${diffDays} day${diffDays !== 1 ? "s" : ""} ago`
}

/**
* Returns true if two dates fall on the same calendar day.
*/
export function isSameDay(a: Date, b: Date): boolean {
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
)
}

/**
* Add a number of days to a date (returns a new Date).
*/
export function addDays(date: Date, days: number): Date {
const result = new Date(date)
result.setDate(result.getDate() + days)
return result
}
}
26 changes: 16 additions & 10 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ 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")
}

export function slugify(str: string): string {
return str
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "")
.trim()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-]/g, "")
.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+/).filter(Boolean).length
}

export function truncate(str: string, maxLength: number): string {
if (str.length <= maxLength) return str
const cutoff = maxLength - 3
const slice = str.slice(0, cutoff)
const lastSpace = slice.lastIndexOf(" ")
if (lastSpace === -1) {
return slice + "..."
}
return slice.slice(0, lastSpace) + "..."
}
49 changes: 32 additions & 17 deletions src/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Task {
}

export class TaskManager {
private tasks: Map<string, Task> = new Map()
private tasks: Task[] = []
private nextId = 1

add(title: string, priority: Priority = "medium", description?: string): Task {
Expand All @@ -29,43 +29,58 @@ export class TaskManager {
status: "pending",
createdAt: new Date(),
}
this.tasks.set(task.id, task)
this.tasks.push(task)
return task
}

get(id: string): Task | undefined {
return this.tasks.get(id)
return this.tasks.find((t) => t.id === id)
}

list(filter?: { status?: Status; priority?: Priority }): Task[] {
let result = Array.from(this.tasks.values())
if (filter?.status) result = result.filter((t) => t.status === filter.status)
if (filter?.priority) result = result.filter((t) => t.priority === filter.priority)
return result
if (!filter) return [...this.tasks]
return this.tasks.filter((t) => {
if (filter.status && t.status !== filter.status) return false
if (filter.priority && t.priority !== filter.priority) return false
return true
})
}

complete(id: string): boolean {
const task = this.tasks.get(id)
const task = this.get(id)
if (!task) return false
task.status = "completed"
task.completedAt = new Date()
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")
const index = this.tasks.findIndex((t) => t.id === id)
if (index === -1) return false
this.tasks.splice(index, 1)
return true
}

// TODO: implement — update title/description/priority of a task
// return 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.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 priorityOrder: Record<Priority, number> = { high: 0, medium: 1, low: 2 }
const statusOrder: Record<Status, number> = { pending: 0, in_progress: 1, completed: 2 }
return [...this.tasks].sort((a, b) => {
if (field === "priority") {
return priorityOrder[a.priority] - priorityOrder[b.priority]
}
if (field === "createdAt") {
return a.createdAt.getTime() - b.createdAt.getTime()
}
return statusOrder[a.status] - statusOrder[b.status]
})
}
}
}
41 changes: 7 additions & 34 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,19 @@
* Input validation utilities.
*/

/**
* 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).
*/
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)
*/
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 === ""
} catch {
return false
}
return /^https?:\/\/[^\s/$.?#].[^\s]*$/.test(value)
}

/**
* Returns true if the string contains only alphanumeric characters and underscores,
* starts with a letter, and is between minLen and maxLen characters.
*/
export function isUsername(value: string, minLen = 3, maxLen = 20): boolean {
if (value.length < minLen || value.length > maxLen) return false
return /^[a-zA-Z][a-zA-Z0-9_]*$/.test(value)
export function isUsername(value: string): boolean {
return /^[a-zA-Z_][a-zA-Z0-9_]{2,19}$/.test(value)
}

/**
* Returns true if the string is a valid Australian phone number.
* Accepts formats: 04XX XXX XXX, +614XX XXX XXX, (02) XXXX XXXX, 02 XXXX XXXX
*/
export function isAustralianPhone(value: string): boolean {
const cleaned = value.replace(/[\s\-().]/g, "")
return /^(\+?61|0)[2-578]\d{8}$/.test(cleaned)
}
const stripped = value.replace(/[\s().+-]/g, "")
return /^(61|0)[2-9]\d{8}$/.test(stripped)
}