Skip to content

Commit c6ace03

Browse files
committed
refactor: make task fields optional and update parsing logic to handle undefined values
1 parent 4733bd2 commit c6ace03

9 files changed

Lines changed: 498 additions & 269 deletions

File tree

‎src/parse.test.ts‎

Lines changed: 110 additions & 114 deletions
Large diffs are not rendered by default.

‎src/parse.ts‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,6 @@ export function parse<TScenario extends string = string>(
174174
}
175175
}
176176

177-
if (!type) {
178-
throw new Error("Invalid task format: missing task type");
179-
}
180-
181177
// Extract additional focuses that appear after metadata but before dates
182178
// They can appear anywhere in the remaining text
183179
// Convert remaining to array once and reuse it
@@ -250,12 +246,14 @@ export function parse<TScenario extends string = string>(
250246
}
251247

252248
// Extract priority
253-
let priority: Priority = "normal";
249+
let priority: Priority | undefined;
254250
if (remaining.includes("🔺")) priority = "maximum";
255251
else if (remaining.includes("⏫")) priority = "high";
256252
else if (remaining.includes("🔼")) priority = "medium";
257253
else if (remaining.includes("🔽")) priority = "low";
258254
else if (remaining.includes("⏬")) priority = "minimum";
255+
// Only set 'normal' as default when validation is enabled and other fields exist
256+
// Otherwise leave it undefined to match the markdown standard
259257

260258
// Extract recurrence
261259
let recurrence: string | undefined;
@@ -322,17 +320,14 @@ export function parse<TScenario extends string = string>(
322320
const task: Partial<Task> & {
323321
state: TaskState;
324322
content: string;
325-
type: TaskType;
326-
tags: string[];
327-
priority: Priority;
328323
} = {
329324
state,
330325
content,
331-
type,
332-
tags,
333-
priority,
326+
tags, // Always include tags (as array, even if empty)
334327
};
335328

329+
if (type) task.type = type;
330+
if (priority) task.priority = priority;
336331
if (id) task.id = id;
337332
if (focuses.length > 0) task.focuses = focuses;
338333
if (scenarios.length > 0) task.scenarios = scenarios;

‎src/schema.test.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe("validateTask", () => {
99
content: "Valid task",
1010
type: "quick",
1111
tags: ["Tasks/Quick"],
12-
priority: "normal",
1312
};
1413
expect(validateTask(task)).toBe(true);
1514
});
@@ -21,7 +20,6 @@ describe("validateTask", () => {
2120
type: "quick",
2221
tags: ["Tasks/Quick"],
2322
duration: "invalid",
24-
priority: "normal",
2523
};
2624
expect(validateTask(task)).toBe(false);
2725
});
@@ -33,7 +31,6 @@ describe("validateTask", () => {
3331
type: "quick",
3432
tags: ["Tasks/Quick"],
3533
dueAt: "15-01-2025",
36-
priority: "normal",
3734
};
3835
expect(validateTask(task)).toBe(false);
3936
});
@@ -45,7 +42,6 @@ describe("validateTask", () => {
4542
type: "quick",
4643
tags: ["Tasks/Quick"],
4744
time: { start: "9:00", end: "10:30" },
48-
priority: "normal",
4945
};
5046
expect(validateTask(task)).toBe(false);
5147
});
@@ -56,7 +52,6 @@ describe("validateTask", () => {
5652
content: "",
5753
type: "quick",
5854
tags: ["Tasks/Quick"],
59-
priority: "normal",
6055
};
6156
expect(validateTask(task)).toBe(false);
6257
});
@@ -98,7 +93,6 @@ describe("isValidTask", () => {
9893
content: "Valid task",
9994
type: "quick",
10095
tags: ["Tasks/Quick"],
101-
priority: "normal",
10296
};
10397

10498
if (isValidTask(task)) {
@@ -117,7 +111,6 @@ describe("isValidTask", () => {
117111
type: "quick",
118112
tags: ["Tasks/Quick"],
119113
duration: "invalid",
120-
priority: "normal",
121114
};
122115
expect(isValidTask(task)).toBe(false);
123116
});
@@ -138,7 +131,6 @@ describe("validateTaskWithErrors", () => {
138131
content: "Valid task",
139132
type: "quick",
140133
tags: ["Tasks/Quick"],
141-
priority: "normal",
142134
};
143135
const result = validateTaskWithErrors(task);
144136
expect(result.success).toBe(true);
@@ -154,7 +146,6 @@ describe("validateTaskWithErrors", () => {
154146
type: "quick",
155147
tags: ["Tasks/Quick"],
156148
duration: "invalid",
157-
priority: "normal",
158149
};
159150
const result = validateTaskWithErrors(task);
160151
expect(result.success).toBe(false);
@@ -173,7 +164,6 @@ describe("validateTaskWithErrors", () => {
173164
type: "quick",
174165
tags: ["Tasks/Quick"],
175166
dueAt: "15-01-2025",
176-
priority: "normal",
177167
};
178168
const result = validateTaskWithErrors(task);
179169
expect(result.success).toBe(false);
@@ -192,7 +182,6 @@ describe("validateTaskWithErrors", () => {
192182
type: "quick",
193183
tags: ["Tasks/Quick"],
194184
time: { start: "9:00", end: "10:30" },
195-
priority: "normal",
196185
};
197186
const result = validateTaskWithErrors(task);
198187
expect(result.success).toBe(false);
@@ -207,7 +196,6 @@ describe("validateTaskWithErrors", () => {
207196
content: "",
208197
type: "quick",
209198
tags: ["Tasks/Quick"],
210-
priority: "normal",
211199
};
212200
const result = validateTaskWithErrors(task);
213201
expect(result.success).toBe(false);

‎src/schema.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export const HooksSchema = z.object({
8080
export const TaskSchema = z.object({
8181
state: TaskStateSchema,
8282
content: z.string().min(1),
83-
type: TaskTypeSchema,
84-
tags: z.array(z.string()),
83+
type: TaskTypeSchema.optional(),
84+
tags: z.array(z.string()).optional(),
8585
id: z.string().optional(),
8686
focuses: z.array(FocusSchema).optional(),
8787
scenarios: z.array(z.string()).optional(),
@@ -96,7 +96,7 @@ export const TaskSchema = z.object({
9696
completedAt: DateFieldSchema,
9797
cancelledAt: DateFieldSchema,
9898
time: TimeSchema.optional(),
99-
priority: PrioritySchema.default("normal"),
99+
priority: PrioritySchema.optional(),
100100
recurrence: z.string().optional(),
101101
dependencies: z.array(z.string()).optional(),
102102
hooks: HooksSchema.optional(),

0 commit comments

Comments
 (0)