From aa52a31e902798c33c6280d88381c75c0a7de471 Mon Sep 17 00:00:00 2001 From: harttle Date: Thu, 16 Mar 2017 10:39:10 +0800 Subject: [PATCH] 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);