Skip to content

Commit 5b7dffa

Browse files
author
Niklas Kiefer
committed
feat(condition-checker): add #evaluate
1 parent 3cce402 commit 5b7dffa

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

packages/form-js-viewer/src/core/ConditionChecker.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { unaryTest } from 'feelin';
1+
import { unaryTest, evaluate } from 'feelin';
22
import { isString } from 'min-dash';
33

44
/**
@@ -81,6 +81,33 @@ export class ConditionChecker {
8181
return result === true;
8282
}
8383

84+
/**
85+
* Evaluate an expression.
86+
*
87+
* @param {string} expression
88+
* @param {import('../types').Data} [data]
89+
*
90+
* @returns {any}
91+
*/
92+
evaluate(expression, data = {}) {
93+
if (!expression) {
94+
return null;
95+
}
96+
97+
if (!isString(expression) || !expression.startsWith('=')) {
98+
return null;
99+
}
100+
101+
try {
102+
const result = evaluate(expression.slice(1), data);
103+
104+
return result;
105+
} catch (error) {
106+
this._eventBus.fire('error', { error });
107+
return null;
108+
}
109+
}
110+
84111
_getConditions() {
85112
const formFields = this._formFieldRegistry.getAll();
86113

packages/form-js-viewer/test/spec/core/ConditionChecker.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,87 @@ describe('ConditionChecker', function() {
122122
});
123123

124124

125+
describe('#evaluate', function() {
126+
127+
it('should return null if there is no expression', function() {
128+
129+
// when
130+
const result = conditionChecker.evaluate(null, null, false);
131+
132+
// then
133+
expect(result).to.be.null;
134+
});
135+
136+
137+
it('should return null for non-string expression', function() {
138+
139+
// given
140+
const expression = 1;
141+
142+
// when
143+
const result = conditionChecker.evaluate(expression);
144+
145+
// then
146+
expect(result).to.be.null;
147+
});
148+
149+
150+
it('should return null if expression does not start with =', function() {
151+
152+
// given
153+
const expression = 'foo';
154+
155+
// when
156+
const result = conditionChecker.evaluate(expression);
157+
158+
// then
159+
expect(result).to.be.null;
160+
});
161+
162+
163+
it('should return null and report error if condition has syntax error', function() {
164+
165+
// given
166+
const expression = '=foo-';
167+
168+
// when
169+
const result = conditionChecker.evaluate(expression);
170+
171+
// then
172+
expect(result).to.be.null;
173+
expect(fireSpy).to.have.been.calledWith('error');
174+
expect(fireSpy.args[0][1].error).to.be.instanceof(Error);
175+
});
176+
177+
178+
it('should return expression result', function() {
179+
180+
// given
181+
const expression = '=2 + 2 + 5';
182+
183+
// when
184+
const result = conditionChecker.evaluate(expression);
185+
186+
// then
187+
expect(result).to.equal(9);
188+
});
189+
190+
191+
it('should return expression result (with data)', function() {
192+
193+
// given
194+
const expression = '=2 + 2 + five';
195+
196+
// when
197+
const result = conditionChecker.evaluate(expression, { five: 5 });
198+
199+
// then
200+
expect(result).to.equal(9);
201+
});
202+
203+
});
204+
205+
125206
describe('#applyConditions', function() {
126207

127208
it('should filter out properties for which condition is not met', function() {

0 commit comments

Comments
 (0)