joewalnes / filtrex

A simple, safe, JavaScript Filter Expression compiler for end-users
MIT License
1.05k stars 74 forks source link

Expressions with Spaces #14

Open jfinch1991 opened 7 years ago

jfinch1991 commented 7 years ago

Im trying to get this to parse expressions with spaces e.g. Net Income > 10000. I have tried a few things like wrapping with quotes "Net Income" but that hasn't worked. Any ideas?

msantos commented 7 years ago

Use single quotes:

var e = "'a b' > 3"                                                             
var f = compileExpression(e)                                                    
f({"a b": 1}) // 0                                                              
f({"a b": 4}) // 1
summer9090 commented 7 years ago

met the same issue, tried using quotes, but it doesn't work, showing: Unrecognized text error

Any advice?

msantos commented 7 years ago

If you want help, show the code that isn't working.

To debug this yourself, open a page that loads filtrex. Then open the javascript console and enter in the code from https://github.com/joewalnes/filtrex/issues/14#issuecomment-282844804. Change the expression to match your input and re-test.

summer9090 commented 7 years ago

I am using the same example you replied @jfinch1991 , the code is as follows:

function hello() { var e = "'a b' > 3" var myfilter = compileExpression(e); myfilter({"a b": 2}); }

Uncaught Error: Lexical error on line 1. Unrecognized text. 'a b' > 3 ^

Thanks for advice

msantos commented 7 years ago

Works for me. Try running the tests, in particular check the test for "quoted symbols" passes. To be sure, you can add a test case with whitespace:

diff --git a/test/filtrex-test.html b/test/filtrex-test.html
index d0ba6ed..49a5afe 100644
--- a/test/filtrex-test.html
+++ b/test/filtrex-test.html
@@ -128,6 +128,7 @@ tests({

     'quoted symbols': function() {
         eq(123, compileExpression('\'hello-world-foo\'')({'hello-world-foo': 123}));
+        eq(123, compileExpression('\'hello world foo\'')({'hello world foo': 123}));
         eq(123, compileExpression('\'order+goo*and#stuff\'')({'order+goo*and#stuff': 123}));
     },

If the tests work, verify your version of filtrex.js is up to date.

summer9090 commented 7 years ago

Thanks for your reply.

I cloned this project and run the tests, all passed. But in my project, I installed filtrex(version: 0.5.4) through npm. When I run the same test('quoted symbol'), it will pop up the errors above. Is the filtrex version in node repository the latest one?

Thanks.

summer9090 commented 7 years ago

Any updates about the issue?

Thanks

cshaa commented 4 years ago

The issue was caused by NPM not being up-to-date. As of 2019 this is no longer the case and the NPM package is being actively maintained again.