Skip to content

Commit

Permalink
Add support nested properties and arrays into condition (visibleIf):#177
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jan 5, 2017
1 parent 35750e2 commit 3dd92f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {HashTable} from './base';
import {ConditionsParser} from './conditionsParser';
import {ProcessValue} from "./conditionProcessValue";

export class Condition {
static operatorsValue: HashTable<Function> = null;
Expand Down Expand Up @@ -65,11 +66,13 @@ export class ConditionNode {
}
export class ConditionRunner {
private expressionValue: string;
private processValue: ProcessValue;
private root: ConditionNode;
private values: HashTable<any>;
public constructor(expression: string) {
this.root = new ConditionNode();
this.expression = expression;
this.processValue = new ProcessValue();
}
public get expression(): string { return this.expressionValue; }
public set expression(value: string) {
Expand Down Expand Up @@ -100,14 +103,14 @@ export class ConditionRunner {
var left = condition.left;
var name = this.getValueName(left);
if (name) {
if (!(name in this.values)) return false;
left = this.values[name];
if (!this.processValue.hasValue(name, this.values)) return false;
left = this.processValue.getValue(name, this.values);
}
var right = condition.right;
name = this.getValueName(right);
if (name) {
if (!(name in this.values)) return false;
right = this.values[name];
if (!this.processValue.hasValue(name, this.values)) return false;
right = this.processValue.getValue(name, this.values);
}
return condition.perform(left, right);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/conditionstests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ QUnit.test("Run complex condition", function (assert) {
assert.equal(runner.run(values), false, "20 >= 21 and (male = male or 2 > 1)");
});

QUnit.test("Run condition with nested properties", function (assert) {
var runner = new ConditionRunner("{age.min} >= 35 and ({age.max} <= 80");
var values = { age: { min: 36, max: 40 } };
assert.equal(runner.run(values), true, "min > 35 max < 80");
values.age.min = 21;
assert.equal(runner.run(values), false, "min < 35 max < 80");
});


QUnit.test("Expression Tree to Text", function (assert) {
var parser = new ConditionsParser();
var node = new ConditionNode();
Expand Down

0 comments on commit 3dd92f4

Please sign in to comment.