Skip to content

Instantly share code, notes, and snippets.

@bonomiandreia
Created November 8, 2024 12:50
Show Gist options
  • Save bonomiandreia/824f25cdcce6f1cba6d19a4fbbe50434 to your computer and use it in GitHub Desktop.
Save bonomiandreia/824f25cdcce6f1cba6d19a4fbbe50434 to your computer and use it in GitHub Desktop.
todoStudy
function removeTodo(todoList: Todo[], id: number): Todo[] {
throwErrors(todoList, id);
let newList = todoList.filter((item: Todo) => {
return item.id !== id
})
return newList;
}
function toogleTodo(todoList: Todo[], id: number): Todo[] {
throwErrors(todoList, id);
let toogledList = todoList.map((item: Todo) => {
if (item.id === id) {
item.completed = !item.completed
}
return item;
})
return toogledList;
}
function getPendingTodos(todoList: Todo[]): Todo[] {
let newList = todoList.filter((item) => {
return item.completed === false;
})
return newList;
}
function checkIfTodoExist(todoList: Todo[], id: number): boolean {
return todoList.some(item => item.id === id)
}
function throwErrors(todoList: Todo[], id: number) {
if (!checkIfTodoExist(todoList, id)) {
throw new TypeError('todo id doesnt exist');
}
}
function returnOnlyTodosAfterOrEqual(todoList: Todo[], date: Date) {
const dateOnly = new Date(date);
dateOnly.setHours(0, 0, 0, 0);
let newList = todoList.filter(item => item.dueDate && item.dueDate >= dateOnly)
return newList;
}
let newItem = {id: 3, title: "test", completed: false, dueDate: new Date('11/11/2024')}
todos = addTodo(todos, newItem);
todos = removeTodo(todos, 1)
todos = toogleTodo(todos, 3)
let todoAfterToday = returnOnlyTodosAfterOrEqual(todos, new Date());
console.log(todoAfterToday)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment