From 9df188ad0320fac33c57065eb1d166e8c7713158 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Fri, 20 Jan 2023 08:04:22 -0800 Subject: feat(wpcarro/slx): Support numeric comparisons I was (and still ~am) a bit leery of supporting this (scope creep?), but I need it in two of my personal projects all within the first O(days) of using this. So I'm thinking that if this tool is going to be a workhorse, I'll need to sacrifice some purity for practicality. Future me will find out the real answer... Change-Id: Ia71a8cf6627062440476b638d2c194c2c9ac97c4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7878 Autosubmit: wpcarro Reviewed-by: wpcarro Tested-by: BuildkiteCI --- users/wpcarro/slx.js/index.js | 68 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'users/wpcarro/slx.js') diff --git a/users/wpcarro/slx.js/index.js b/users/wpcarro/slx.js/index.js index 9f2ed675f7a9..44f2f3643f8c 100644 --- a/users/wpcarro/slx.js/index.js +++ b/users/wpcarro/slx.js/index.js @@ -49,6 +49,19 @@ function compile(ast, config) { }; } } + if (ast.type === 'COMPARE_SELECTION') { + const f = compile(ast.val, config); + + let compare = null; + if (ast.operator === 'LT') { compare = (x, y) => x < y; } + if (ast.operator === 'GT') { compare = (x, y) => x > y; } + if (ast.operator === 'LTE') { compare = (x, y) => x <= y; } + if (ast.operator === 'GTE') { compare = (x, y) => x >= y; } + + return function(row) { + return ast.negate ? !compare(row[ast.key], ast.val) : compare(row[ast.key], ast.val); + }; + } if (ast.type === 'SELECTION') { const f = compile(ast.val, config); return function(row) { @@ -116,6 +129,17 @@ function tokenize(x) { i += 1; continue; } + // Tokenize numbers (i.e. integers, floats). + if (/[0-9]/.test(x[i])) { + let curr = x[i]; + i += 1; + while (i < x.length && /[0-9]/.test(x[i])) { + curr += x[i]; + i += 1; + } + result.push(['NUMBER', parseFloat(curr)]); + continue; + } if (ATOM_REGEX.test(x[i])) { let curr = x[i]; i += 1; @@ -126,6 +150,26 @@ function tokenize(x) { result.push(['ATOM', curr]); continue; } + if (x[i] === '<' && i + 1 < x.length && x[i + 1] === '=') { + result.push(['COMPARE', 'LTE']); + i += 1; + continue; + } + if (x[i] === '<') { + result.push(['COMPARE', 'LT']); + i += 1; + continue; + } + if (x[i] === '>' && i + i < x.length && x[i + 1] === '=') { + result.push(['COMPARE', 'GTE']); + i += 1; + continue; + } + if (x[i] === '>') { + result.push(['COMPARE', 'GT']); + i += 1; + continue; + } if (x[i] === ':') { result.push(['COLON', null]); i += 1; @@ -297,7 +341,29 @@ function selection(p, config) { val, }; } - } else { + } + // column type === 'ATOM', 'a column label', p); + const operator = match((type, _) => type === 'COMPARE', 'a comparison operator (i.e. "<", ">", "<=", ">=")', p); + const val = match((type, _) => type === 'NUMBER', 'a number', p); + + return { + type: 'COMPARE_SELECTION', + operator, + negate, + key, + val, + }; + } + else { return matchAll(p, config); } } -- cgit 1.4.1