Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORK and FIX Nullable values #fix133 #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/lib/evaluator/Evaluator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ describe('Evaluator', () => {
const e = new Evaluator(grammar)
return expect(e.eval(toTree('(2 + 3) * 4'))).resolves.toBe(20)
})
it('evaluates an arithmetic expression with null values', async () => {
const e = new Evaluator(grammar)
return expect(e.eval(toTree('(null + 3) * 4'))).resolves.toBe(null)
})
it('evaluates an arithmetic expression with undefined values', async () => {
const e = new Evaluator(grammar)
return expect(e.eval(toTree('(undefined + 3) * 4'))).resolves.toBe(null)
})
it('evaluates an arithmetic expression with null values in context', async () => {
const context = { nulvar: null }
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('(nulvar + 3) * 4'))).resolves.toBe(null)
})
it('evaluates an arithmetic expression with undefined values in context', async () => {
const context = {}
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('(foo + 3) * 4'))).resolves.toBe(null)
})
it('evaluates an comparision involving arithmetic expression with undefined values in context', async () => {
const context = {}
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('(foo + 3) * 4 == null'))).resolves.toBe(true)
})
it('evaluates an comparision involving arithmetic expression with null values in context', async () => {
const context = { nulvar: null }
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('nulvar * 4 == null'))).resolves.toBe(true)
})
it('evaluates a string concat', async () => {
const e = new Evaluator(grammar)
return expect(e.eval(toTree('"Hello" + (4+4) + "Wo\\"rld"'))).resolves.toBe(
Expand Down
22 changes: 16 additions & 6 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ exports.getGrammar = () => ({
'+': {
type: 'binaryOp',
precedence: 30,
eval: (left, right) => left + right
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : left + right
},
'-': {
type: 'binaryOp',
precedence: 30,
eval: (left, right) => left - right
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : left - right
},
'*': {
type: 'binaryOp',
precedence: 40,
eval: (left, right) => left * right
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : left * right
},
'/': {
type: 'binaryOp',
Expand All @@ -46,17 +49,20 @@ exports.getGrammar = () => ({
'//': {
type: 'binaryOp',
precedence: 40,
eval: (left, right) => Math.floor(left / right)
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : Math.floor(left / right)
},
'%': {
type: 'binaryOp',
precedence: 50,
eval: (left, right) => left % right
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : left % right
},
'^': {
type: 'binaryOp',
precedence: 50,
eval: (left, right) => Math.pow(left, right)
eval: (left, right) =>
isNulable(left) || isNulable(right) ? null : Math.pow(left, right)
},
'==': {
type: 'binaryOp',
Expand Down Expand Up @@ -165,3 +171,7 @@ exports.getGrammar = () => ({
*/
transforms: {}
})

function isNulable(val) {
return val == null || val == undefined || val == NaN
}