示例工程 51-test-pyramid
51-test-pyramid/app/.tsc/shared/todo.js
export function addTodo(todos, title) {
const value = title.trim();
if (value.length === 0)
throw new Error("BAD_TITLE:empty");
if (value.length > 80)
throw new Error("BAD_TITLE:too-long");
const nextId = todos.reduce((max, todo) => Math.max(max, todo.id), 0) + 1;
return [...todos, { id: nextId, title: value, done: false }];
}
export function toggleTodo(todos, id) {
return todos.map((todo) => todo.id === id ? { ...todo, done: !todo.done } : todo);
}
export function visibleTodos(todos, filter) {
if (filter === "active")
return todos.filter((todo) => !todo.done);
if (filter === "done")
return todos.filter((todo) => todo.done);
return [...todos];
}
export function parseAddRequest(value) {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_ADD_REQUEST");
}
const title = value.title;
if (typeof title !== "string")
throw new Error("BAD_TITLE:not-string");
if (title.trim().length === 0)
throw new Error("BAD_TITLE:empty");
if (title.length > 80)
throw new Error("BAD_TITLE:too-long");
}
export function parseToggleRequest(value) {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_TOGGLE_REQUEST");
}
const id = value.id;
if (typeof id !== "number" || !Number.isInteger(id) || id <= 0) {
throw new Error("BAD_ID");
}
}
//# sourceMappingURL=todo.js.map