Skip to content
Source
Sign inSign up
TanStack/showcase
A read-only demo of every feature
Every page of this repository is open, and nothing can change. Take a guided tour of what TanStack Source does.

feat: renameTask changes the title of a task #6

OpenAlemTuzlakwants to merge 2 commits intomain fromfeat/rename-taskopened

Showing 3 changed files with 15 additions and 2 deletions.

x
0 of 3 viewed, 3 left
1 addition and 1 deletionsrc/index.tsModified
View file
@@ -1,2 +1,2 @@
-export { addTask, completeTask, openTasks, removeTask, type Task, type TaskList } from './tasks.ts'
+export { addTask, completeTask, openTasks, removeTask, renameTask, type Task, type TaskList } from './tasks.ts'
export { formatList, progress } from './format.ts'
7 additions and 0 deletionssrc/tasks.tsModified
View file
@@ -36,3 +36,10 @@
export function openTasks(list: TaskList): TaskList {
return list.filter((task) => !task.done).toSorted((a, b) => a.priority - b.priority)
+​
+/** Gives the task `id` a new title. Throws when the new title is empty. */
+export function renameTask(list: TaskList, id: number, title: string): TaskList {
+ const name = title.trim()
+ if (name === '') throw new Error('A task needs a title.')
+ return list.map((task) => (task.id === id ? { ...task, title: name } : task))
+}
7 additions and 1 deletiontest/tasks.test.tsModified
View file
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { addTask, completeTask, formatList, openTasks, progress, removeTask } from '../src/index.ts'
+import { addTask, completeTask, formatList, openTasks, progress, removeTask, renameTask } from '../src/index.ts'
​
describe('tasks', () => {
it('gives each new task the next id', () => {
@@ -16,6 +16,12 @@
expect(openTasks(list).map((task) => task.title)).toEqual(['Fix the bug', 'Docs'])
})