Skip to content

Commit

Permalink
Support substring function into expression fix #7816 (#7817)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Feb 6, 2024
1 parent a3fe155 commit fd73ac7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/functionsfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,15 @@ function propertyValue(params: any[]): any {
const q = getQuestionValueByContext(this, params[0]);
return q ? q[params[1]] : undefined;
}
FunctionFactory.Instance.register("propertyValue", propertyValue);
FunctionFactory.Instance.register("propertyValue", propertyValue);
function substring_(params: any[]): any {
if(params.length < 2) return "";
const s = params[0];
if(!s || typeof s !== "string") return "";
const start = params[1];
if(!Helpers.isNumber(start)) return "";
const end = params.length > 2 ? params[2] : undefined;
if(!Helpers.isNumber(end)) return s.substring(start);
return s.substring(start, end);
}
FunctionFactory.Instance.register("substring", substring_);
9 changes: 9 additions & 0 deletions tests/expressions/expressionParserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1509,3 +1509,12 @@ QUnit.test("Custom function returns object&array, #7050", function(assert) {
FunctionFactory.Instance.unregister("func1");
FunctionFactory.Instance.unregister("func2");
});
QUnit.test("ExpressionRunner: substring", function(assert) {
var runner = new ExpressionRunner("substring({s}, 1, 3)");
const values: any = { s: "abcdef" };
assert.equal(runner.run(values), "bc", "abcdef");
values.s = undefined;
assert.equal(runner.run(values), "", "undefined");
values.s = 10;
assert.equal(runner.run(values), "", "10");
});

0 comments on commit fd73ac7

Please sign in to comment.