Skip to content

Commit

Permalink
fix negative number bug
Browse files Browse the repository at this point in the history
  • Loading branch information
William committed Apr 29, 2019
1 parent 6b1f755 commit 7774faf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions __tests__/evaluate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('evaluate expression', () => {
it('== & !=', () => {
expect(evaluate('1 == 1')).toEqual(true);
expect(evaluate('2 == 1')).toEqual(false);
expect(evaluate('1 != -1')).toEqual(true);
expect(evaluate('1 == "1"')).toEqual(true);
expect(evaluate('1 != 1')).toEqual(false);
expect(evaluate('2 != 1')).toEqual(true);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-expression-evaluator",
"version": "1.3.12",
"version": "1.3.13",
"description": "Context-based expression parse and evaluator.",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down Expand Up @@ -99,4 +99,4 @@
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/lib/Handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export const Handlers: Handlers = {
throw new Error();
},

UnaryExpression(ast: t.Expression, context: Context): boolean {
UnaryExpression(ast: t.Expression, context: Context): boolean | number {
if (t.isUnaryExpression(ast)) {
switch (ast.operator) {
case '!':
return !evaluate(ast.argument, context);
case '-':
return - evaluate(ast.argument, context);
}
}
throw new Error();
Expand Down

0 comments on commit 7774faf

Please sign in to comment.