From 7ad9ce95e9267b0c71842b11cdcbbd9141fbe571 Mon Sep 17 00:00:00 2001 From: ChenL Date: Wed, 15 Mar 2017 20:38:08 +0800 Subject: [PATCH 1/4] Illegal expression in contains --- src/operators.js | 2 +- test/syntax.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/operators.js b/src/operators.js index e233416e2..11d44fe2f 100644 --- a/src/operators.js +++ b/src/operators.js @@ -5,7 +5,7 @@ var operators = { '<': (l, r) => l < r, '>=': (l, r) => l >= r, '<=': (l, r) => l <= r, - 'contains': (l, r) => l.indexOf(r) > -1, + 'contains': (l, r) => typeof l === 'string' ? l.indexOf(r) > -1 : false, 'and': (l, r) => l && r, 'or': (l, r) => l || r }; diff --git a/test/syntax.js b/test/syntax.js index b57a85b6b..f0226a640 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -13,7 +13,8 @@ describe('expression', function() { scope = Scope.factory({ one: 1, two: 2, - x: 'XXX' + x: 'XXX', + y: undefined }); }); @@ -25,6 +26,7 @@ describe('expression', function() { it('should throw on illegal expression', function() { expect(function() { evalExp('1 contains "x"', scope); + evalExp('y contains "x"', scope); }).to.throw(); }); From f1d68409b18f285d5b958824dee2d1a8b9de3c4a Mon Sep 17 00:00:00 2001 From: ChenL Date: Wed, 15 Mar 2017 21:20:00 +0800 Subject: [PATCH 2/4] Do not throw error in contains when variable is illegal. --- test/syntax.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/test/syntax.js b/test/syntax.js index f0226a640..02cbe54dd 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -23,13 +23,6 @@ describe('expression', function() { expect(evalValue('"foo"')).to.equal("foo"); }); - it('should throw on illegal expression', function() { - expect(function() { - evalExp('1 contains "x"', scope); - evalExp('y contains "x"', scope); - }).to.throw(); - }); - it('should eval variables', function() { expect(evalValue('23', scope)).to.equal(23); expect(evalValue('one', scope)).to.equal(1); @@ -50,6 +43,8 @@ describe('expression', function() { expect(evalExp('one<=two', scope)).to.equal(true); expect(evalExp('x contains "x"', scope)).to.equal(false); expect(evalExp('x contains "X"', scope)).to.equal(true); + expect(evalExp('1 contains "x"', scope)).to.equal(false); + expect(evalExp('y contains "x"', scope)).to.equal(false); expect(evalExp('"<=" == "<="', scope)).to.equal(true); }); From e4056a79ee91067256a68a5c573cf5ccdf7fc2af Mon Sep 17 00:00:00 2001 From: ChenL Date: Thu, 16 Mar 2017 10:05:50 +0800 Subject: [PATCH 3/4] Test if an array contains a specific number/string. --- src/operators.js | 8 +++++++- test/syntax.js | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/operators.js b/src/operators.js index 11d44fe2f..f43cfa576 100644 --- a/src/operators.js +++ b/src/operators.js @@ -5,7 +5,13 @@ var operators = { '<': (l, r) => l < r, '>=': (l, r) => l >= r, '<=': (l, r) => l <= r, - 'contains': (l, r) => typeof l === 'string' ? l.indexOf(r) > -1 : false, + 'contains': (l, r) => { + return typeof l !== 'undefined' + ? typeof l.indexOf === 'function' + ? l.indexOf(r) > -1 + : false + : false; + }, 'and': (l, r) => l && r, 'or': (l, r) => l || r }; diff --git a/test/syntax.js b/test/syntax.js index 02cbe54dd..1426d8f5b 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -45,6 +45,8 @@ describe('expression', function() { expect(evalExp('x contains "X"', scope)).to.equal(true); expect(evalExp('1 contains "x"', scope)).to.equal(false); expect(evalExp('y contains "x"', scope)).to.equal(false); + expect(evalExp('(1..5) contains 3', scope)).to.equal(true); + expect(evalExp('(1..5) contains 6', scope)).to.equal(false); expect(evalExp('"<=" == "<="', scope)).to.equal(true); }); From aa52a31e902798c33c6280d88381c75c0a7de471 Mon Sep 17 00:00:00 2001 From: harttle Date: Thu, 16 Mar 2017 10:39:10 +0800 Subject: [PATCH 4/4] fix: contains return false when left side equals null --- src/operators.js | 10 ++++------ test/syntax.js | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/operators.js b/src/operators.js index f43cfa576..ba93de411 100644 --- a/src/operators.js +++ b/src/operators.js @@ -5,12 +5,10 @@ var operators = { '<': (l, r) => l < r, '>=': (l, r) => l >= r, '<=': (l, r) => l <= r, - 'contains': (l, r) => { - return typeof l !== 'undefined' - ? typeof l.indexOf === 'function' - ? l.indexOf(r) > -1 - : false - : false; + 'contains': (l, r) => { + if (!l) return false; + if (typeof l.indexOf !== 'function') return false; + return l.indexOf(r) > -1; }, 'and': (l, r) => l && r, 'or': (l, r) => l || r diff --git a/test/syntax.js b/test/syntax.js index 1426d8f5b..1b47aa646 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -14,7 +14,8 @@ describe('expression', function() { one: 1, two: 2, x: 'XXX', - y: undefined + y: undefined, + z: null }); }); @@ -45,6 +46,7 @@ describe('expression', function() { expect(evalExp('x contains "X"', scope)).to.equal(true); expect(evalExp('1 contains "x"', scope)).to.equal(false); expect(evalExp('y contains "x"', scope)).to.equal(false); + expect(evalExp('z contains "x"', scope)).to.equal(false); expect(evalExp('(1..5) contains 3', scope)).to.equal(true); expect(evalExp('(1..5) contains 6', scope)).to.equal(false); expect(evalExp('"<=" == "<="', scope)).to.equal(true);