Skip to content

Commit 969f3a0

Browse files
committed
refactor(scripts): extend shared JS modules for status maps and filters
status_maps.js: - Add taskStatusMapAbbrev for compact output (dump_database) - Add projectStatusMapAbbrev for compact output filters.js: - Add isWithinDays(date, days, requirePastOrPresent) helper - Refactor date range checks to use new helper
1 parent 63c4e9e commit 969f3a0

2 files changed

Lines changed: 49 additions & 38 deletions

File tree

src/omnifocus_mcp/scripts/common/filters.js

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
// Shared between search.js and browse.js
33
// Requires: status_maps.js to be loaded first
44

5+
/**
6+
* Check if a date falls within N days from now.
7+
* @param {Date|null} date - The date to check
8+
* @param {number} days - Number of days in the future
9+
* @param {boolean} requirePastOrPresent - If true, date must also be >= now (for due dates)
10+
* @returns {boolean} True if date is within range
11+
*/
12+
function isWithinDays(date, days, requirePastOrPresent) {
13+
if (!date) {
14+
return false;
15+
}
16+
var now = new Date();
17+
var futureDate = new Date();
18+
futureDate.setDate(futureDate.getDate() + days);
19+
20+
if (date > futureDate) {
21+
return false;
22+
}
23+
if (requirePastOrPresent && date < now) {
24+
return false;
25+
}
26+
return true;
27+
}
28+
529
/**
630
* Create a filter function for tasks.
731
* @param {Object} filters - Filter criteria
@@ -67,39 +91,21 @@ function createTaskFilter(filters, options) {
6791

6892
// Filter by due_within N days
6993
if (filters.due_within !== undefined) {
70-
if (!task.dueDate) {
71-
return false;
72-
}
73-
var now = new Date();
74-
var futureDate = new Date();
75-
futureDate.setDate(futureDate.getDate() + filters.due_within);
76-
if (task.dueDate > futureDate || task.dueDate < now) {
94+
if (!isWithinDays(task.dueDate, filters.due_within, true)) {
7795
return false;
7896
}
7997
}
8098

8199
// Filter by deferred_until N days
82100
if (filters.deferred_until !== undefined) {
83-
if (!task.deferDate) {
84-
return false;
85-
}
86-
var now = new Date();
87-
var futureDate = new Date();
88-
futureDate.setDate(futureDate.getDate() + filters.deferred_until);
89-
if (task.deferDate > futureDate) {
101+
if (!isWithinDays(task.deferDate, filters.deferred_until, false)) {
90102
return false;
91103
}
92104
}
93105

94106
// Filter by planned_within N days (OmniFocus 4.7+)
95107
if (filters.planned_within !== undefined) {
96-
if (!task.plannedDate) {
97-
return false;
98-
}
99-
var now = new Date();
100-
var futureDate = new Date();
101-
futureDate.setDate(futureDate.getDate() + filters.planned_within);
102-
if (task.plannedDate > futureDate || task.plannedDate < now) {
108+
if (!isWithinDays(task.plannedDate, filters.planned_within, true)) {
103109
return false;
104110
}
105111
}
@@ -185,26 +191,14 @@ function createProjectFilter(filters, options) {
185191

186192
// Filter by due_within N days
187193
if (filters.due_within !== undefined) {
188-
if (!project.dueDate) {
189-
return false;
190-
}
191-
var now = new Date();
192-
var futureDate = new Date();
193-
futureDate.setDate(futureDate.getDate() + filters.due_within);
194-
if (project.dueDate > futureDate || project.dueDate < now) {
194+
if (!isWithinDays(project.dueDate, filters.due_within, true)) {
195195
return false;
196196
}
197197
}
198198

199199
// Filter by deferred_until N days
200200
if (filters.deferred_until !== undefined) {
201-
if (!project.deferDate) {
202-
return false;
203-
}
204-
var now = new Date();
205-
var futureDate = new Date();
206-
futureDate.setDate(futureDate.getDate() + filters.deferred_until);
207-
if (project.deferDate > futureDate) {
201+
if (!isWithinDays(project.deferDate, filters.deferred_until, false)) {
208202
return false;
209203
}
210204
}

src/omnifocus_mcp/scripts/common/status_maps.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Status mappings for OmniFocus entities
2-
// Shared between search.js and browse.js
2+
// Shared between search.js, browse.js, dump_database.js, and get_perspective_view.js
33

4-
// Task status enum to string mapping
4+
// Task status enum to string mapping (full names)
55
var taskStatusMap = {};
66
taskStatusMap[Task.Status.Available] = "Available";
77
taskStatusMap[Task.Status.Blocked] = "Blocked";
@@ -11,13 +11,30 @@ taskStatusMap[Task.Status.DueSoon] = "DueSoon";
1111
taskStatusMap[Task.Status.Next] = "Next";
1212
taskStatusMap[Task.Status.Overdue] = "Overdue";
1313

14-
// Project status enum to string mapping
14+
// Project status enum to string mapping (full names)
1515
var projectStatusMap = {};
1616
projectStatusMap[Project.Status.Active] = "Active";
1717
projectStatusMap[Project.Status.Done] = "Done";
1818
projectStatusMap[Project.Status.Dropped] = "Dropped";
1919
projectStatusMap[Project.Status.OnHold] = "OnHold";
2020

21+
// Abbreviated task status map (for compact output like dump_database)
22+
var taskStatusMapAbbrev = {};
23+
taskStatusMapAbbrev[Task.Status.Available] = "avail";
24+
taskStatusMapAbbrev[Task.Status.Blocked] = "block";
25+
taskStatusMapAbbrev[Task.Status.Completed] = "compl";
26+
taskStatusMapAbbrev[Task.Status.Dropped] = "drop";
27+
taskStatusMapAbbrev[Task.Status.DueSoon] = "due";
28+
taskStatusMapAbbrev[Task.Status.Next] = "next";
29+
taskStatusMapAbbrev[Task.Status.Overdue] = "over";
30+
31+
// Abbreviated project status map (for compact output)
32+
var projectStatusMapAbbrev = {};
33+
projectStatusMapAbbrev[Project.Status.Active] = "active";
34+
projectStatusMapAbbrev[Project.Status.Done] = "done";
35+
projectStatusMapAbbrev[Project.Status.Dropped] = "dropped";
36+
projectStatusMapAbbrev[Project.Status.OnHold] = "onHold";
37+
2138
// Normalize user-provided status strings to canonical form
2239
var statusNameMap = {
2340
// Task statuses

0 commit comments

Comments
 (0)