filterquery.mjs 5.41 KB
Newer Older
ransome1's avatar
ransome1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// This is a simple stack machine that executes a filter language query
// compiled by filterlang.pegjs (which generates filterlang.mjs).
// The compiled query consists of a list of postfix opcodes designed
// specifically for todo.txt searching and filtering.

function runQuery(item, compiledQuery) {
  if (!compiledQuery) {
    return true;  // a null query matches everything
  }
  let stack = [];
  let operand1 = false;
  let operand2 = false;
  let next = 0;
  let q = compiledQuery.slice(); // shallow copy
  while (q.length > 0) {
    const opcode = q.shift();
    switch(opcode) {
zerodat's avatar
zerodat committed
18
      case "pri":
ransome1's avatar
ransome1 committed
19
20
21
        stack.push(item.priority);
        break;
      case "due":
zerodat's avatar
zerodat committed
22
        if (item.due) {
ransome1's avatar
ransome1 committed
23
24
          // normalize date to have time of midnight in local zone
          // we represent dates as millisec from epoch to simplify comparison
zerodat's avatar
zerodat committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
          let d = item.due;
          stack.push(new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime());
        } else {
          stack.push(undefined);  // all comparisons will return false
        }
        break;
      case "duestr":
        // match next value (a string) as prefix of ISO date string of due date
        next = q.shift(); // the string to compare
        if (item.due) {
          // normalize date to have time of midnight in local zone
          // we represent dates as millisec from epoch to simplify comparison
          let d = item.due;
          d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
          stack.push(d.toISOString().slice(0, 10).startsWith(next));
        } else {
          stack.push(false);  // no due date
ransome1's avatar
ransome1 committed
42
43
        }
        break;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      case "threshold":
        if (item.t) {
          // normalize date to have time of midnight in local zone
          // we represent dates as millisec from epoch to simplify comparison
          let d = item.t;
          stack.push(new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime());
        } else {
          stack.push(undefined);  // all comparisons will return false
        }
        break;
      case "tstr":
        // match next value (a string) as prefix of ISO date string of threshold date
        next = q.shift(); // the string to compare
        if (item.t) {
          // normalize date to have time of midnight in local zone
          // we represent dates as millisec from epoch to simplify comparison
          let d = item.t;
          d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
          stack.push(d.toISOString().slice(0, 10).startsWith(next));
        } else {
          stack.push(false);  // no threshold date
        }
        break;
ransome1's avatar
ransome1 committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
      case "complete":
        stack.push(item.complete);
        break;
      case "string":
        next = q.shift();  // the string value to match
        stack.push(item.toString().toLowerCase().indexOf(next.toLowerCase()) !== -1);
        break;
      case "regex":
        next = q.shift();  // the regex to match
        stack.push(next.test(item.toString()));
        break;
      case "==":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 == operand2);
        break;
      case "!=":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 != operand2);
        break;
      case "<":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 < operand2);
        break;
      case "<=":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 <= operand2);
        break;
      case ">":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 > operand2);
        break;
      case ">=":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 >= operand2);
        break;
      case "++":
        next = q.shift();
        if (next == "*") {
          stack.push(item.projects ? true : false);
        } else if (next.startsWith('"')) {
          stack.push(item.projects && item.projects.includes(next.slice(1,-1)));
        } else {
zerodat's avatar
zerodat committed
115
116
          // case-insensitive match for next as a substring of the project name
          let pattern = next.toLowerCase();
ransome1's avatar
ransome1 committed
117
          stack.push(item.projects && item.projects.findIndex(function(p) {
zerodat's avatar
zerodat committed
118
            return p.toLowerCase().indexOf(pattern) > -1;
ransome1's avatar
ransome1 committed
119
120
121
122
123
124
125
126
127
128
          }) > -1);
        }
        break;
      case "@@":
        next = q.shift();
        if (next == "*") {
          stack.push(item.contexts ? true : false);
        } else if (next.startsWith('"')) {
          stack.push(item.contexts && item.contexts.includes(next.slice(1,-1)));
        } else {
zerodat's avatar
zerodat committed
129
130
          // case-insensitive match for next as a substring of the context name
          let pattern = next.toLowerCase();
ransome1's avatar
ransome1 committed
131
          stack.push(item.contexts && item.contexts.findIndex(function(c) {
zerodat's avatar
zerodat committed
132
            return c.toLowerCase().indexOf(pattern) > -1;
ransome1's avatar
ransome1 committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
          }) > -1);
        }
        break;
      case "||":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 || operand2);
        break;
      case "&&":
        operand2 = stack.pop();
        operand1 = stack.pop();
        stack.push(operand1 && operand2);
        break;
      case "!!":
        operand1 = stack.pop();
        stack.push(!operand1);
        break;
      default:
        // should be a data item like a string or date in millisec, ...
        stack.push(opcode);
        break;
    }
  }
  return stack.pop();
}

export { runQuery };