fix: contains return false when left side equals null

This commit is contained in:
harttle
2017-03-16 10:39:10 +08:00
parent e4056a79ee
commit aa52a31e90
2 changed files with 7 additions and 7 deletions
+4 -6
View File
@@ -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
+3 -1
View File
@@ -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);