@openmrs/esm-form-engine-lib 4.3.1-pre.2701 → 4.3.1-pre.2707
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,2 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
3
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
4
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
5
|
+
* with `useFieldValue('1stVisit')`.
|
|
6
|
+
*
|
|
7
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
8
|
+
* `field === undefined` both test whether the field has a value.
|
|
9
|
+
*
|
|
10
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
11
|
+
*
|
|
12
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
13
|
+
*/
|
|
1
14
|
export declare function evaluatePostSubmissionExpression(expression: string, encounters: any[]): boolean;
|
|
2
15
|
//# sourceMappingURL=post-submission-action-helper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post-submission-action-helper.d.ts","sourceRoot":"","sources":["../../../src/utils/post-submission-action-helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"post-submission-action-helper.d.ts","sourceRoot":"","sources":["../../../src/utils/post-submission-action-helper.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,OAAO,CAgB/F"}
|
|
@@ -1,57 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
import { compile, evaluate, extractVariableNames } from "@openmrs/esm-framework";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
4
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
5
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
6
|
+
* with `useFieldValue('1stVisit')`.
|
|
7
|
+
*
|
|
8
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
9
|
+
* `field === undefined` both test whether the field has a value.
|
|
10
|
+
*
|
|
11
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
12
|
+
*
|
|
13
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
14
|
+
*/ export function evaluatePostSubmissionExpression(expression, encounters) {
|
|
2
15
|
const encounter = encounters[0];
|
|
3
|
-
const regx = /(?:\w+|'(?:\\'|[^'\n])*')/g;
|
|
4
|
-
let match;
|
|
5
|
-
const fieldIds = new Set();
|
|
6
16
|
try {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const isFloat = /^-?\d+\.\d+$/.test(value);
|
|
12
|
-
if (!(value.startsWith("'") && value.endsWith("'")) && typeof value === 'string' && !isBoolean && !isNumber && !isFloat) {
|
|
13
|
-
fieldIds.add(value);
|
|
14
|
-
}
|
|
17
|
+
const ast = compile(expression);
|
|
18
|
+
const variables = {};
|
|
19
|
+
for (const name of extractVariableNames(ast)){
|
|
20
|
+
variables[name] = getFieldValue(name, encounter);
|
|
15
21
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
fieldToValueMap = getFieldValues(fieldIds, encounter);
|
|
20
|
-
}
|
|
21
|
-
if (Object.keys(fieldToValueMap).length) {
|
|
22
|
-
replacedExpression = expression.replace(/(\w+)/g, (match)=>{
|
|
23
|
-
return Object.prototype.hasOwnProperty.call(fieldToValueMap, match) ? fieldToValueMap[match] : match;
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
replacedExpression = expression;
|
|
27
|
-
}
|
|
28
|
-
return eval(replacedExpression);
|
|
22
|
+
variables.useFieldValue = (fieldId)=>getFieldValue(fieldId, encounter);
|
|
23
|
+
variables.undefined = null;
|
|
24
|
+
return Boolean(evaluate(ast, variables));
|
|
29
25
|
} catch (error) {
|
|
30
|
-
throw new Error('Error evaluating expression'
|
|
26
|
+
throw new Error('Error evaluating expression', {
|
|
27
|
+
cause: error
|
|
28
|
+
});
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
function
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
let value = encounter.obs?.find((item)=>item.formFieldPath.includes(fieldId))?.value;
|
|
37
|
-
if (typeof value === 'object') {
|
|
38
|
-
value = value.uuid;
|
|
39
|
-
}
|
|
40
|
-
if (value) {
|
|
41
|
-
value = formatValue(value);
|
|
42
|
-
}
|
|
43
|
-
result[fieldId] = value;
|
|
44
|
-
});
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
//This function wraps string values in single quotes which Javascript will evaluate
|
|
48
|
-
function formatValue(value) {
|
|
49
|
-
if (typeof value === 'string') {
|
|
50
|
-
if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
51
|
-
return value;
|
|
52
|
-
} else {
|
|
53
|
-
return `'${value}'`;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return value;
|
|
31
|
+
function getFieldValue(fieldId, encounter) {
|
|
32
|
+
const value = encounter.obs?.find((item)=>item.formFieldPath.includes(fieldId))?.value;
|
|
33
|
+
return (typeof value === 'object' ? value?.uuid : value) ?? null;
|
|
57
34
|
}
|
package/package.json
CHANGED
|
@@ -1,71 +1,37 @@
|
|
|
1
|
+
import { compile, evaluate, extractVariableNames, type VariablesMap } from '@openmrs/esm-framework';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
5
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
6
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
7
|
+
* with `useFieldValue('1stVisit')`.
|
|
8
|
+
*
|
|
9
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
10
|
+
* `field === undefined` both test whether the field has a value.
|
|
11
|
+
*
|
|
12
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
13
|
+
*
|
|
14
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
15
|
+
*/
|
|
1
16
|
export function evaluatePostSubmissionExpression(expression: string, encounters: any[]): boolean {
|
|
2
17
|
const encounter = encounters[0];
|
|
3
|
-
const regx = /(?:\w+|'(?:\\'|[^'\n])*')/g;
|
|
4
|
-
let match;
|
|
5
|
-
const fieldIds = new Set<string>();
|
|
6
18
|
try {
|
|
7
|
-
|
|
8
|
-
const value = match[0].replace(/\\'/g, "'"); // Replace escaped single quotes
|
|
19
|
+
const ast = compile(expression);
|
|
9
20
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (
|
|
15
|
-
!(value.startsWith("'") && value.endsWith("'")) &&
|
|
16
|
-
typeof value === 'string' &&
|
|
17
|
-
!isBoolean &&
|
|
18
|
-
!isNumber &&
|
|
19
|
-
!isFloat
|
|
20
|
-
) {
|
|
21
|
-
fieldIds.add(value);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
let fieldToValueMap = {};
|
|
26
|
-
let replacedExpression;
|
|
27
|
-
if (fieldIds.size) {
|
|
28
|
-
fieldToValueMap = getFieldValues(fieldIds, encounter);
|
|
21
|
+
const variables: VariablesMap = {};
|
|
22
|
+
for (const name of extractVariableNames(ast)) {
|
|
23
|
+
variables[name] = getFieldValue(name, encounter);
|
|
29
24
|
}
|
|
25
|
+
variables.useFieldValue = (fieldId: string) => getFieldValue(fieldId, encounter);
|
|
26
|
+
variables.undefined = null;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
replacedExpression = expression.replace(/(\w+)/g, (match) => {
|
|
33
|
-
return Object.prototype.hasOwnProperty.call(fieldToValueMap, match) ? fieldToValueMap[match] : match;
|
|
34
|
-
});
|
|
35
|
-
} else {
|
|
36
|
-
replacedExpression = expression;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return eval(replacedExpression);
|
|
28
|
+
return Boolean(evaluate(ast, variables));
|
|
40
29
|
} catch (error) {
|
|
41
|
-
throw new Error('Error evaluating expression');
|
|
30
|
+
throw new Error('Error evaluating expression', { cause: error });
|
|
42
31
|
}
|
|
43
32
|
}
|
|
44
33
|
|
|
45
|
-
function
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
let value = encounter.obs?.find((item) => item.formFieldPath.includes(fieldId))?.value;
|
|
49
|
-
if (typeof value === 'object') {
|
|
50
|
-
value = value.uuid;
|
|
51
|
-
}
|
|
52
|
-
if (value) {
|
|
53
|
-
value = formatValue(value);
|
|
54
|
-
}
|
|
55
|
-
result[fieldId] = value;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
//This function wraps string values in single quotes which Javascript will evaluate
|
|
62
|
-
function formatValue(value: any): any {
|
|
63
|
-
if (typeof value === 'string') {
|
|
64
|
-
if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
65
|
-
return value;
|
|
66
|
-
} else {
|
|
67
|
-
return `'${value}'`;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return value;
|
|
34
|
+
function getFieldValue(fieldId: string, encounter: any) {
|
|
35
|
+
const value = encounter.obs?.find((item) => item.formFieldPath.includes(fieldId))?.value;
|
|
36
|
+
return (typeof value === 'object' ? value?.uuid : value) ?? null;
|
|
71
37
|
}
|