@openmrs/esm-form-engine-lib 2.1.0-pre.1502 → 2.1.0-pre.1511
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.
- package/3a3e1d216bd6470d/3a3e1d216bd6470d.gz +0 -0
- package/__mocks__/forms/rfe-forms/bmi-test-form.json +66 -66
- package/__mocks__/forms/rfe-forms/bsa-test-form.json +66 -66
- package/__mocks__/forms/rfe-forms/edd-test-form.json +87 -87
- package/__mocks__/forms/rfe-forms/external_data_source_form.json +35 -36
- package/__mocks__/forms/rfe-forms/historical-expressions-form.json +1 -1
- package/__mocks__/forms/rfe-forms/labour_and_delivery_test_form.json +1 -1
- package/__mocks__/forms/rfe-forms/months-on-art-form.json +89 -89
- package/__mocks__/forms/rfe-forms/next-visit-test-form.json +77 -77
- package/b3059e748360776a/b3059e748360776a.gz +0 -0
- package/dist/openmrs-esm-form-engine-lib.js +1 -1
- package/package.json +1 -1
- package/src/components/inputs/date/date.test.tsx +107 -0
- package/src/components/inputs/number/number.component.tsx +2 -1
- package/src/components/inputs/ui-select-extended/ui-select-extended.component.tsx +1 -1
- package/src/components/inputs/ui-select-extended/ui-select-extended.test.tsx +5 -0
- package/src/processors/encounter/encounter-form-processor.ts +13 -14
- package/src/types/schema.ts +0 -2
- package/src/utils/common-expression-helpers.test.ts +74 -5
- package/src/utils/common-expression-helpers.ts +21 -26
- package/src/utils/expression-runner.test.ts +156 -69
- package/src/utils/expression-runner.ts +85 -135
- package/aaf8197a12df0c40/aaf8197a12df0c40.gz +0 -0
- package/aba5c979c0dbf1c7/aba5c979c0dbf1c7.gz +0 -0
- package/src/utils/expression-parser.test.ts +0 -308
- package/src/utils/expression-parser.ts +0 -158
- /package/{47245761e3f779c4/47245761e3f779c4.gz → 22ec231da647cd2c/22ec231da647cd2c.gz} +0 -0
- /package/{6f1d94035d69e5e1/6f1d94035d69e5e1.gz → 485e0040f135cee8/485e0040f135cee8.gz} +0 -0
@@ -1,158 +0,0 @@
|
|
1
|
-
import { type FormField } from '../types';
|
2
|
-
import { ConceptFalse, ConceptTrue } from '../constants';
|
3
|
-
import { registerDependency } from './common-expression-helpers';
|
4
|
-
import { type FormNode } from './expression-runner';
|
5
|
-
|
6
|
-
/**
|
7
|
-
* Parses a complex expression string into an array of tokens, ignoring operators found within quotes and within parentheses.
|
8
|
-
*
|
9
|
-
* @param expression The expression string to parse.
|
10
|
-
* @returns An array of tokens representing the individual elements of the expression.
|
11
|
-
*/
|
12
|
-
export function parseExpression(expression: string): string[] {
|
13
|
-
const tokens = [];
|
14
|
-
let currentToken = '';
|
15
|
-
let inQuote = false;
|
16
|
-
let openParensCount = 0;
|
17
|
-
|
18
|
-
for (let i = 0; i < expression.length; i++) {
|
19
|
-
const char = expression.charAt(i);
|
20
|
-
|
21
|
-
if (char === "'" || char === '"') {
|
22
|
-
if (inQuote) {
|
23
|
-
inQuote = false;
|
24
|
-
} else if (openParensCount === 0) {
|
25
|
-
inQuote = true;
|
26
|
-
}
|
27
|
-
}
|
28
|
-
if (inQuote) {
|
29
|
-
currentToken += char;
|
30
|
-
} else {
|
31
|
-
if (char === '(') {
|
32
|
-
openParensCount++;
|
33
|
-
} else if (char === ')') {
|
34
|
-
openParensCount--;
|
35
|
-
}
|
36
|
-
if (openParensCount === 0) {
|
37
|
-
if (char === ' ' || char === '\t' || char === '\n') {
|
38
|
-
if (currentToken.length > 0) {
|
39
|
-
tokens.push(currentToken);
|
40
|
-
currentToken = '';
|
41
|
-
}
|
42
|
-
} else {
|
43
|
-
currentToken += char;
|
44
|
-
}
|
45
|
-
} else {
|
46
|
-
currentToken += char;
|
47
|
-
}
|
48
|
-
}
|
49
|
-
}
|
50
|
-
if (currentToken.length > 0) {
|
51
|
-
tokens.push(currentToken);
|
52
|
-
}
|
53
|
-
return tokens;
|
54
|
-
}
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Links field references within expression fragments to the actual field values
|
58
|
-
* @returns The expression with linked field references
|
59
|
-
*/
|
60
|
-
export function linkReferencedFieldValues(
|
61
|
-
fields: FormField[],
|
62
|
-
fieldValues: Record<string, any>,
|
63
|
-
tokens: string[],
|
64
|
-
): string {
|
65
|
-
const processedTokens = [];
|
66
|
-
tokens.forEach((token) => {
|
67
|
-
if (hasParentheses(token)) {
|
68
|
-
let tokenWithUnresolvedArgs = token;
|
69
|
-
extractArgs(token).forEach((arg) => {
|
70
|
-
const referencedField = findReferencedFieldIfExists(arg, fields);
|
71
|
-
if (referencedField) {
|
72
|
-
tokenWithUnresolvedArgs = replaceFieldRefWithValuePath(
|
73
|
-
referencedField,
|
74
|
-
fieldValues[referencedField.id],
|
75
|
-
tokenWithUnresolvedArgs,
|
76
|
-
);
|
77
|
-
}
|
78
|
-
});
|
79
|
-
processedTokens.push(tokenWithUnresolvedArgs);
|
80
|
-
} else {
|
81
|
-
const referencedField = findReferencedFieldIfExists(token, fields);
|
82
|
-
if (referencedField) {
|
83
|
-
processedTokens.push(replaceFieldRefWithValuePath(referencedField, fieldValues[referencedField.id], token));
|
84
|
-
} else {
|
85
|
-
// push token as is
|
86
|
-
processedTokens.push(token);
|
87
|
-
}
|
88
|
-
}
|
89
|
-
});
|
90
|
-
return processedTokens.join(' ');
|
91
|
-
}
|
92
|
-
|
93
|
-
/**
|
94
|
-
* Extracts the arguments or parameters to a function within an arbitrary expression.
|
95
|
-
*
|
96
|
-
* @param {string} expression - The expression to extract arguments from.
|
97
|
-
* @returns {string[]} An array of the extracted arguments.
|
98
|
-
*/
|
99
|
-
export function extractArgs(expression: string): string[] {
|
100
|
-
const args = [];
|
101
|
-
// eslint-disable-next-line no-useless-escape
|
102
|
-
const regx = /(?:\w+|'(?:\\'|[^'\n])*')(?=[,\)]|\s*(?=\)))/g;
|
103
|
-
let match;
|
104
|
-
while ((match = regx.exec(expression))) {
|
105
|
-
args.push(match[0].replace(/\\'/g, "'").replace(/(^'|'$)/g, ''));
|
106
|
-
}
|
107
|
-
return args;
|
108
|
-
}
|
109
|
-
|
110
|
-
/**
|
111
|
-
* Checks if an expression contains opening and closing parentheses.
|
112
|
-
*
|
113
|
-
* @param {string} expression - The expression to check.
|
114
|
-
* @returns {boolean} `true` if the expression contains parentheses, otherwise `false`.
|
115
|
-
*/
|
116
|
-
export function hasParentheses(expression: string): boolean {
|
117
|
-
const re = /[()]/;
|
118
|
-
return re.test(expression);
|
119
|
-
}
|
120
|
-
|
121
|
-
export function replaceFieldRefWithValuePath(field: FormField, value: any, token: string): string {
|
122
|
-
if (token.includes(`useFieldValue('${field.id}')`)) {
|
123
|
-
return token;
|
124
|
-
}
|
125
|
-
// strip quotes
|
126
|
-
token = token.replace(new RegExp(`['"]${field.id}['"]`, 'g'), field.id);
|
127
|
-
if (field.questionOptions.rendering == 'toggle' && typeof value == 'boolean') {
|
128
|
-
// TODO: reference ConceptTrue and ConceptFalse through config patterns
|
129
|
-
return token.replace(field.id, `${value ? `'${ConceptTrue}'` : `'${ConceptFalse}'`}`);
|
130
|
-
}
|
131
|
-
return token.replace(field.id, `fieldValues.${field.id}`);
|
132
|
-
}
|
133
|
-
|
134
|
-
/**
|
135
|
-
* Finds and registers referenced fields in the expression
|
136
|
-
* @param fieldNode The field node
|
137
|
-
* @param tokens Expression tokens
|
138
|
-
* @param fields All fields
|
139
|
-
*/
|
140
|
-
export function findAndRegisterReferencedFields(fieldNode: FormNode, tokens: string[], fields: Array<FormField>): void {
|
141
|
-
tokens.forEach((token) => {
|
142
|
-
if (hasParentheses(token)) {
|
143
|
-
extractArgs(token).forEach((arg) => {
|
144
|
-
registerDependency(fieldNode, findReferencedFieldIfExists(arg, fields));
|
145
|
-
});
|
146
|
-
} else {
|
147
|
-
registerDependency(fieldNode, findReferencedFieldIfExists(token, fields));
|
148
|
-
}
|
149
|
-
});
|
150
|
-
}
|
151
|
-
|
152
|
-
function findReferencedFieldIfExists(fieldId: string, fields: FormField[]): FormField | undefined {
|
153
|
-
// check if field id has trailing quotes
|
154
|
-
if (/^'+|'+$/.test(fieldId)) {
|
155
|
-
fieldId = fieldId.replace(/^'|'$/g, '');
|
156
|
-
}
|
157
|
-
return fields.find((field) => field.id === fieldId);
|
158
|
-
}
|
File without changes
|
File without changes
|