diff options
author | William Carroll <wpcarro@gmail.com> | 2023-01-14T01·35-0800 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-01-17T21·17+0000 |
commit | 032ab16bbbd318704be71af7b569624ddab24802 (patch) | |
tree | 7e0efc8ad45a157e9c400a51cc15d0c169dfae1b /users/wpcarro | |
parent | a9d866c7759bc6ac0ffde5e048c15ca846ca05f4 (diff) |
feat(wpcarro/slx): Support filtering by date r/5682
Queries like `before:"03/05/2020` Change-Id: I34b06f38c8f3abeed9ad6824cd365065a1fe8d2b Reviewed-on: https://cl.tvl.fyi/c/depot/+/7831 Reviewed-by: wpcarro <wpcarro@gmail.com> Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI
Diffstat (limited to 'users/wpcarro')
-rw-r--r-- | users/wpcarro/scratch/simple-select/index.js | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/users/wpcarro/scratch/simple-select/index.js b/users/wpcarro/scratch/simple-select/index.js index 4be5b5e265fe..15a35ab74334 100644 --- a/users/wpcarro/scratch/simple-select/index.js +++ b/users/wpcarro/scratch/simple-select/index.js @@ -2,7 +2,9 @@ const state = { // Match values case sensitively when filtering. caseSensitive: false, // Coerce values into regular expressions (instead of strings) when they're defined as atoms. - preferRegex: false, + preferRegex: true, + // The key in the JS object that hosts the Date type against which we filter. + dateKey: 'Date', }; // TODO(wpcarro): Support filtering by date (before, after). @@ -29,10 +31,40 @@ function compile(ast) { }; } } + if (ast.type === 'DATE_SELECTION') { + if (ast.key === 'before') { + return function(row) { + let t = new Date(); + if (ast.val === 'yesterday') { + t.setDate(t.getDate() - 1); + console.log(t); + } + // MM/DD/YYYY + else { + t = new Date(ast.val); + } + return row[state.dateKey] < t; + }; + } + if (ast.key === 'after') { + return function(row) { + let t = new Date(); + if (ast.val === 'yesterday') { + t.setDate(t.getDate() - 1); + console.log(t); + } + // MM/DD/YYYY + else { + t = new Date(ast.val); + } + return row[state.dateKey] > t; + }; + } + } if (ast.type === 'SELECTION') { const f = compile(ast.val); return function(row) { - return ast.negate ? f(row[ast.key]) : f(row[ast.key]); + return ast.negate ? !f(row[ast.key]) : f(row[ast.key]); }; } if (ast.type === 'MATCH_ALL') { @@ -249,23 +281,34 @@ function peekType(n, p) { function selection(p) { // column:value OR -column:value - if ((peekType(0, p) === 'ATOM' && peekType(1, p) === 'COLON') || (peekType(0, p) === 'NEGATE' && peekType(1, p) === 'ATOM' && peekType(2, p) === 'COLON')) { + let negate = false; if (p.tokens[p.i][0] === 'NEGATE') { negate = true; p.i += 1; } + const key = match((type, _) => type === 'ATOM', 'a column label', p); expect((type, val) => type === 'COLON', 'a colon', p); - const val = value(p); - return { - type: 'SELECTION', - negate, - key, - val, - }; + + if (key === 'before' || key === 'after') { + const val = date(p); + return { + type: 'DATE_SELECTION', + key, + val, + }; + } else { + const val = value(p); + return { + type: 'SELECTION', + negate, + key, + val, + }; + } } else { return matchAll(p); } @@ -320,3 +363,10 @@ function value(p) { } throw `Parse Error: Expected a regular expression or a string, but got: ${p.tokens[p.i]}; ${JSON.stringify(p)}`; } + +function date(p) { + const [type, val] = p.tokens[p.i]; + p.i += 1; + + return val; +} |