33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
const statuses = new Set(['todo', 'doing', 'done', 'blocked']);
|
|
|
|
export function normalizeTasks(input) {
|
|
if (!Array.isArray(input)) throw new TypeError('任务必须是数组');
|
|
if (input.length > 10000) throw new RangeError('任务数量不能超过10000项');
|
|
|
|
const ids = new Set();
|
|
const tasks = [];
|
|
for (const task of input) {
|
|
if (task === null || typeof task !== 'object' || Array.isArray(task)) {
|
|
throw new TypeError('每项任务必须是对象');
|
|
}
|
|
if (typeof task.id !== 'string' || typeof task.title !== 'string') {
|
|
throw new TypeError('任务id和title必须是字符串');
|
|
}
|
|
const id = task.id.trim();
|
|
const title = task.title.trim();
|
|
if (!id || !title) throw new TypeError('任务id和title不能为空');
|
|
if (ids.has(id)) throw new TypeError(`任务id重复:${id}`);
|
|
|
|
const { status, minutes } = task;
|
|
if (!statuses.has(status)) {
|
|
throw new TypeError('任务status必须是todo、doing、done或blocked');
|
|
}
|
|
if (!Number.isSafeInteger(minutes) || minutes < 0 || minutes > 1000000) {
|
|
throw new RangeError('任务minutes必须是0到1000000之间的安全整数');
|
|
}
|
|
ids.add(id);
|
|
tasks.push({ id, title, status, minutes });
|
|
}
|
|
return tasks;
|
|
}
|