From 330048ad6bd7318d0cf9814a6ba35218a2cbfabe Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Thu, 29 Sep 2022 14:23:56 -1000 Subject: [PATCH] [JS] Add the function AFExactMatch --- src/scripting_api/aform.js | 8 ++++++++ test/unit/scripting_spec.js | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/scripting_api/aform.js b/src/scripting_api/aform.js index 95048ea6ba36a..3975c5a7c3a40 100644 --- a/src/scripting_api/aform.js +++ b/src/scripting_api/aform.js @@ -683,6 +683,14 @@ class AForm { eMailValidate(str) { return this._emailRegex.test(str); } + + AFExactMatch(rePatterns, str) { + if (rePatterns instanceof RegExp) { + return str.match(rePatterns)?.[0] === str || 0; + } + + return rePatterns.findIndex(re => str.match(re)?.[0] === str) + 1; + } } export { AForm }; diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index c852021c178ea..08e0ce49650fa 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -1035,7 +1035,7 @@ describe("Scripting", function () { }); }); - describe("ASSimple_Calculate", function () { + describe("AFSimple_Calculate", function () { it("should compute the sum of several fields", async () => { const refIds = [0, 1, 2, 3].map(_ => getId()); const data = { @@ -1429,5 +1429,21 @@ describe("Scripting", function () { expect(value).toEqual(false); }); }); + + describe("AFExactMatch", function () { + it("should check matching between regexs and a string", async () => { + let value = await myeval(`AFExactMatch(/\\d+/, "123")`); + expect(value).toEqual(true); + + value = await myeval(`AFExactMatch(/\\d+/, "foo")`); + expect(value).toEqual(0); + + value = await myeval(`AFExactMatch([/\\d+/, /[fo]*/], "foo")`); + expect(value).toEqual(2); + + value = await myeval(`AFExactMatch([/\\d+/, /[fo]*/], "bar")`); + expect(value).toEqual(0); + }); + }); }); });