Skip to content

Commit

Permalink
fix: Logical operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Sierociński committed Mar 25, 2020
1 parent 22d783b commit 86b7824
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
20 changes: 12 additions & 8 deletions dist/meval.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* meval v1.0.1 | Copyright 2020 (c) Marek Sierociński| https://github.com/marverix/meval/blob/master/LICENSE */
/* meval v1.0.2 | Copyright 2020 (c) Marek Sierociński| https://github.com/marverix/meval/blob/master/LICENSE */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand Down Expand Up @@ -834,15 +834,19 @@
_createClass(MemberAccessOperator, [{
key: "_execute",
value: function _execute(leftSide, rightSide, context) {
leftSide = this.resolveSide(leftSide, context);
rightSide = this.resolveSide(rightSide, false);
var result = leftSide[rightSide];
try {
leftSide = this.resolveSide(leftSide, context);
rightSide = this.resolveSide(rightSide, false);
var result = leftSide[rightSide];

if (typeof result === 'function') {
result = result.bind(leftSide);
}
if (typeof result === 'function') {
result = result.bind(leftSide);
}

return result;
return result;
} catch (e) {
return e;
}
}
}]);

Expand Down
4 changes: 2 additions & 2 deletions dist/meval.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meval",
"version": "1.0.1",
"version": "1.0.2",
"description": "Mimic eval in given context",
"main": "dist/meval.js",
"scripts": {
Expand Down
20 changes: 12 additions & 8 deletions src/operators/MemberAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class MemberAccessOperator extends Abstract2ArgOperator {
}

_execute (leftSide, rightSide, context) {
leftSide = this.resolveSide(leftSide, context);
rightSide = this.resolveSide(rightSide, false);

let result = leftSide[rightSide];
if (typeof result === 'function') {
result = result.bind(leftSide);
try {
leftSide = this.resolveSide(leftSide, context);
rightSide = this.resolveSide(rightSide, false);

let result = leftSide[rightSide];
if (typeof result === 'function') {
result = result.bind(leftSide);
}

return result;
} catch (e) {
return e;
}

return result;
}

}
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ describe('meval', function() {
var ret = meval('false && false', testContext);
expect(ret).to.be.false;
});

it('Should not check right side', function() {
var ret = meval('false && item.x.notExisting', testContext);
expect(ret).to.be.false;
});
});

describe('Test 2 argument operator: ||', function() {
Expand All @@ -398,6 +403,11 @@ describe('meval', function() {
var ret = meval('false || false', testContext);
expect(ret).to.be.false;
});

it('Should not check right side', function() {
var ret = meval('true || item.x.notExisting', testContext);
expect(ret).to.be.true;
});
});

describe('Test mixing 2 argument operators', function() {
Expand Down

0 comments on commit 86b7824

Please sign in to comment.