Skip to content

Commit

Permalink
feat(json-expression): 馃幐 add support for "not" expression
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 25, 2021
1 parent 4e04425 commit ae9c3bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/json-expression/__tests__/codegen.spec.ts
Expand Up @@ -65,3 +65,22 @@ describe('ne', () => {
check(['ne', 3, ['=', '/foo']], false, {foo: 3});
});
});

describe('not', () => {
test('on two literals', () => {
check(['!', ['==', 1, 2]], true);
check(['!', ['==', {foo: 'bar'}, {foo: 'bar'}]], false);
check(['not', ['==', {foo: 'bar'}, {foo: 'baz'}]], true);
check(['not', ['==', [[]], [[]]]], false);
});

test('literal and expression', () => {
check(['!', ['eq', 3, ['=', '/foo']]], true);
check(['not', ['eq', 'bar', ['eq', 1, 1]]], true);
check(['not', ['eq', true, ['eq', 1, 1]]], false);
});

test('together with get', () => {
check(['!', ['eq', 3, ['=', '/foo']]], false, {foo: 3});
});
});
10 changes: 9 additions & 1 deletion src/json-expression/codegen.ts
@@ -1,4 +1,4 @@
import type {Expr, ExprEquals, ExprGet, ExprNotEquals, JsonExpressionCodegenContext, JsonExpressionExecutionContext} from './types';
import type {Expr, ExprEquals, ExprGet, ExprNot, ExprNotEquals, JsonExpressionCodegenContext, JsonExpressionExecutionContext} from './types';
import {Codegen} from '../util/codegen/Codegen';
import {deepEqual} from '../json-equal/deepEqual';
import {toPath, get as get_} from '../json-pointer';
Expand Down Expand Up @@ -112,6 +112,12 @@ export class JsonExpressionCodegen {
return new Expression(`!(${res})`);
}

protected onNot([, a]: ExprNot): ExpressionResult {
const res = this.onExpression(a);
if (res instanceof Literal) return new Literal(!res.val);
return new Expression(`!(${res})`);
}

protected onExpression(expr: Expr | unknown): ExpressionResult {
if (!isExpression(expr)) {
if (expr instanceof Array) {
Expand All @@ -128,6 +134,8 @@ export class JsonExpressionCodegen {
case 'eq': return this.onEquals(expr as ExprEquals);
case '!=':
case 'ne': return this.onNotEquals(expr as ExprNotEquals);
case '!':
case 'not': return this.onNot(expr as ExprNot);
}
return new Literal(false);;
}
Expand Down

0 comments on commit ae9c3bf

Please sign in to comment.