@uipath/apollo-wind 2.92.0 → 2.94.0
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/dist/components/forms/field-renderer.cjs +1 -1
- package/dist/components/forms/field-renderer.js +1 -1
- package/dist/components/forms/field-rules-strings.cjs +40 -0
- package/dist/components/forms/field-rules-strings.d.ts +17 -0
- package/dist/components/forms/field-rules-strings.js +6 -0
- package/dist/components/forms/field-rules.cjs +526 -0
- package/dist/components/forms/field-rules.d.ts +97 -0
- package/dist/components/forms/field-rules.js +468 -0
- package/dist/components/forms/form-designer.cjs +208 -592
- package/dist/components/forms/form-designer.js +210 -594
- package/dist/components/forms/form-schema.d.ts +5 -0
- package/dist/components/forms/index.cjs +43 -3
- package/dist/components/forms/index.d.ts +4 -0
- package/dist/components/forms/index.js +5 -1
- package/dist/components/forms/metadata-form.cjs +1 -1
- package/dist/components/forms/metadata-form.js +1 -1
- package/dist/components/forms/rules-engine-dialect.cjs +644 -0
- package/dist/components/forms/rules-engine-dialect.d.ts +90 -0
- package/dist/components/forms/rules-engine-dialect.js +537 -0
- package/dist/components/forms/rules-engine.cjs +99 -10
- package/dist/components/forms/rules-engine.d.ts +5 -0
- package/dist/components/forms/rules-engine.js +99 -10
- package/dist/components/forms/use-field-rules.cjs +122 -0
- package/dist/components/forms/use-field-rules.d.ts +31 -0
- package/dist/components/forms/use-field-rules.js +88 -0
- package/dist/components/ui/conditions-editor/condition-row.cjs +458 -0
- package/dist/components/ui/conditions-editor/condition-row.d.ts +23 -0
- package/dist/components/ui/conditions-editor/condition-row.js +421 -0
- package/dist/components/ui/conditions-editor/conditions-editor.cjs +363 -0
- package/dist/components/ui/conditions-editor/conditions-editor.d.ts +67 -0
- package/dist/components/ui/conditions-editor/conditions-editor.js +329 -0
- package/dist/components/ui/conditions-editor/context.cjs +45 -0
- package/dist/components/ui/conditions-editor/context.d.ts +54 -0
- package/dist/components/ui/conditions-editor/context.js +8 -0
- package/dist/components/ui/conditions-editor/index.cjs +55 -0
- package/dist/components/ui/conditions-editor/index.d.ts +6 -0
- package/dist/components/ui/conditions-editor/index.js +6 -0
- package/dist/components/ui/conditions-editor/model.cjs +126 -0
- package/dist/components/ui/conditions-editor/model.d.ts +39 -0
- package/dist/components/ui/conditions-editor/model.js +71 -0
- package/dist/components/ui/conditions-editor/status.cjs +198 -0
- package/dist/components/ui/conditions-editor/status.d.ts +44 -0
- package/dist/components/ui/conditions-editor/status.js +131 -0
- package/dist/components/ui/conditions-editor/strings.cjs +86 -0
- package/dist/components/ui/conditions-editor/strings.d.ts +62 -0
- package/dist/components/ui/conditions-editor/strings.js +52 -0
- package/dist/components/ui/conditions-editor/vocabulary.cjs +163 -0
- package/dist/components/ui/conditions-editor/vocabulary.d.ts +51 -0
- package/dist/components/ui/conditions-editor/vocabulary.js +129 -0
- package/dist/components/ui/data-table.cjs +493 -104
- package/dist/components/ui/data-table.d.ts +88 -5
- package/dist/components/ui/data-table.js +491 -105
- package/dist/components/ui/editable-cell.cjs +91 -27
- package/dist/components/ui/editable-cell.d.ts +14 -1
- package/dist/components/ui/editable-cell.js +91 -27
- package/dist/components/ui/field-actions/index.cjs +9 -2
- package/dist/components/ui/field-actions/index.d.ts +1 -0
- package/dist/components/ui/field-actions/index.js +2 -1
- package/dist/components/ui/field-actions/insert-variable-action.cjs +3 -1
- package/dist/components/ui/field-actions/insert-variable-action.d.ts +4 -1
- package/dist/components/ui/field-actions/insert-variable-action.js +3 -1
- package/dist/components/ui/field-actions/rules-action.cjs +244 -0
- package/dist/components/ui/field-actions/rules-action.d.ts +42 -0
- package/dist/components/ui/field-actions/rules-action.js +207 -0
- package/dist/components/ui/index.cjs +282 -243
- package/dist/components/ui/index.d.ts +3 -0
- package/dist/components/ui/index.js +3 -0
- package/dist/components/ui/quick-form-field/components/field-header.cjs +137 -167
- package/dist/components/ui/quick-form-field/components/field-header.d.ts +1 -1
- package/dist/components/ui/quick-form-field/components/field-header.js +138 -168
- package/dist/components/ui/quick-form-field/components/header-actions.cjs +187 -0
- package/dist/components/ui/quick-form-field/components/header-actions.d.ts +59 -0
- package/dist/components/ui/quick-form-field/components/header-actions.js +138 -0
- package/dist/components/ui/quick-form-field/components/header-context.cjs +59 -0
- package/dist/components/ui/quick-form-field/components/header-context.d.ts +33 -0
- package/dist/components/ui/quick-form-field/components/header-context.js +16 -0
- package/dist/components/ui/quick-form-field/index.cjs +18 -4
- package/dist/components/ui/quick-form-field/index.d.ts +2 -0
- package/dist/components/ui/quick-form-field/index.js +3 -1
- package/dist/components/ui/quick-form-field/quick-form-field.cjs +1 -1
- package/dist/components/ui/quick-form-field/quick-form-field.d.ts +6 -6
- package/dist/components/ui/quick-form-field/quick-form-field.js +1 -1
- package/dist/components/ui/quick-form-field/types.d.ts +42 -11
- package/dist/components/ui/row-groups/add-row-button.cjs +54 -0
- package/dist/components/ui/row-groups/add-row-button.d.ts +10 -0
- package/dist/components/ui/row-groups/add-row-button.js +20 -0
- package/dist/components/ui/row-groups/after-press.cjs +73 -0
- package/dist/components/ui/row-groups/after-press.d.ts +7 -0
- package/dist/components/ui/row-groups/after-press.js +36 -0
- package/dist/components/ui/row-groups/collapsible-row-group.cjs +83 -0
- package/dist/components/ui/row-groups/collapsible-row-group.d.ts +13 -0
- package/dist/components/ui/row-groups/collapsible-row-group.js +49 -0
- package/dist/components/ui/row-groups/empty-rows.cjs +42 -0
- package/dist/components/ui/row-groups/empty-rows.d.ts +7 -0
- package/dist/components/ui/row-groups/empty-rows.js +8 -0
- package/dist/components/ui/row-groups/group-mode-toggle.cjs +80 -0
- package/dist/components/ui/row-groups/group-mode-toggle.d.ts +26 -0
- package/dist/components/ui/row-groups/group-mode-toggle.js +40 -0
- package/dist/components/ui/row-groups/index.cjs +62 -0
- package/dist/components/ui/row-groups/index.d.ts +6 -0
- package/dist/components/ui/row-groups/index.js +7 -0
- package/dist/components/ui/row-groups/row-delete-button.cjs +54 -0
- package/dist/components/ui/row-groups/row-delete-button.d.ts +18 -0
- package/dist/components/ui/row-groups/row-delete-button.js +20 -0
- package/dist/components/ui/row-groups/use-row-focus.cjs +82 -0
- package/dist/components/ui/row-groups/use-row-focus.d.ts +17 -0
- package/dist/components/ui/row-groups/use-row-focus.js +48 -0
- package/dist/components/ui/rules-editor/context.cjs +45 -0
- package/dist/components/ui/rules-editor/context.d.ts +52 -0
- package/dist/components/ui/rules-editor/context.js +8 -0
- package/dist/components/ui/rules-editor/index.cjs +56 -0
- package/dist/components/ui/rules-editor/index.d.ts +4 -0
- package/dist/components/ui/rules-editor/index.js +4 -0
- package/dist/components/ui/rules-editor/model.cjs +205 -0
- package/dist/components/ui/rules-editor/model.d.ts +128 -0
- package/dist/components/ui/rules-editor/model.js +144 -0
- package/dist/components/ui/rules-editor/rule-card.cjs +470 -0
- package/dist/components/ui/rules-editor/rule-card.d.ts +14 -0
- package/dist/components/ui/rules-editor/rule-card.js +436 -0
- package/dist/components/ui/rules-editor/rules-editor.cjs +160 -0
- package/dist/components/ui/rules-editor/rules-editor.d.ts +66 -0
- package/dist/components/ui/rules-editor/rules-editor.js +126 -0
- package/dist/components/ui/rules-editor/strings.cjs +68 -0
- package/dist/components/ui/rules-editor/strings.d.ts +56 -0
- package/dist/components/ui/rules-editor/strings.js +34 -0
- package/dist/index.cjs +132 -8
- package/dist/index.d.ts +19 -6
- package/dist/index.js +11 -4
- package/dist/styles.css +119 -24
- package/package.json +2 -2
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import jsep from 'jsep';
|
|
2
|
+
import { type Condition, type ConditionField, type ConditionGroup, type ConditionOperators, type ConditionVocabulary } from '../ui/conditions-editor';
|
|
3
|
+
import type { ExpressionRule, PreservedRule, Rule, RuleRepair, RulesDialect } from '../ui/rules-editor';
|
|
4
|
+
import { type FieldRulesStrings } from './field-rules-strings';
|
|
5
|
+
/** What a field's value is at run time, so fixed values are compared as the same type. */
|
|
6
|
+
export type FieldValueType = 'string' | 'number' | 'boolean';
|
|
7
|
+
/**
|
|
8
|
+
* A field the rules can compare. Its `value` is the key the form's values use, and should be an
|
|
9
|
+
* identifier path such as `amount` or `user.role`: a row only an expression can say, such as
|
|
10
|
+
* Greater than or one with an expression value, cannot name any other key, so it is flagged.
|
|
11
|
+
*/
|
|
12
|
+
export interface FieldRuleField extends ConditionField {
|
|
13
|
+
/** Defaults to `number` for number fields and `string` for the rest. */
|
|
14
|
+
valueType?: FieldValueType;
|
|
15
|
+
}
|
|
16
|
+
export interface RulesEngineVocabularyOptions {
|
|
17
|
+
/** The operators to adapt, such as the default ones with translated labels. */
|
|
18
|
+
operators?: ConditionOperators;
|
|
19
|
+
strings?: Partial<FieldRulesStrings>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* What a ConditionsEditor or RulesEditor offers for rules RulesEngine runs: the operators, with
|
|
23
|
+
* text matching and option lists taking a fixed value only, and a comparison only an expression
|
|
24
|
+
* can store flagged on a field key no expression can name, such as `due-date`. There is no
|
|
25
|
+
* Variable mode: RulesEngine only sees the form's values. Pass translated operators and strings
|
|
26
|
+
* to translate it.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createRulesEngineVocabulary({ operators, strings, }?: RulesEngineVocabularyOptions): Pick<ConditionVocabulary, 'operators' | 'valueModes'>;
|
|
29
|
+
/** `createRulesEngineVocabulary()`, in English. */
|
|
30
|
+
export declare const rulesEngineVocabulary: Pick<ConditionVocabulary, "valueModes" | "operators">;
|
|
31
|
+
/** `rulesEngineVocabulary`'s operators. */
|
|
32
|
+
export declare const RULES_ENGINE_OPERATORS: ConditionOperators;
|
|
33
|
+
export declare function valueTypeOf(field: ConditionField | undefined): FieldValueType;
|
|
34
|
+
/** A fixed value as the type the field holds at run time. */
|
|
35
|
+
export declare function typedValue(field: ConditionField | undefined, text: string): unknown;
|
|
36
|
+
/** A stored fixed value back as the text the editor shows. */
|
|
37
|
+
export declare function valueText(value: unknown): string;
|
|
38
|
+
export declare function printLiteral(value: unknown): string;
|
|
39
|
+
export declare function canName(key: string): boolean;
|
|
40
|
+
/** Expression text for a node, parenthesising every compound operand so precedence never shifts. */
|
|
41
|
+
export declare function printNode(node: jsep.Expression): string;
|
|
42
|
+
export declare function parseExpression(text: string): jsep.Expression | undefined;
|
|
43
|
+
export declare const emptyCheck: (name: string) => string;
|
|
44
|
+
export declare const notEmptyCheck: (name: string) => string;
|
|
45
|
+
/**
|
|
46
|
+
* A complete condition as an expression, or `undefined` when RulesEngine cannot say it: text
|
|
47
|
+
* matching, an empty check on a field of type `other`, an expression value that does not parse,
|
|
48
|
+
* or a field key that is not a path of identifiers.
|
|
49
|
+
*/
|
|
50
|
+
export declare function conditionExpression(c: Condition, vocabulary: ConditionVocabulary): string | undefined;
|
|
51
|
+
/** Whether RulesEngine can run the expression: it parses, and uses only what RulesEngine reads. */
|
|
52
|
+
export declare function canRun(expression: string, fields: readonly ConditionField[]): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The field keys an expression names, as RulesEngine reads them. A function it calls, such as
|
|
55
|
+
* `String`, and the globals `undefined`, `NaN` and `Infinity` name no field. Text that does not
|
|
56
|
+
* parse names none.
|
|
57
|
+
*/
|
|
58
|
+
export declare function fieldKeysOf(expression: string): string[];
|
|
59
|
+
/**
|
|
60
|
+
* The expression with field `from`, and paths into it, named `to`. Text that names neither, or
|
|
61
|
+
* does not parse, comes back unchanged, as does any when an expression cannot name `to`; renamed
|
|
62
|
+
* text is printed again, so its spacing and parentheses follow the printer.
|
|
63
|
+
*/
|
|
64
|
+
export declare function renameInExpression(expression: string, from: string, to: string): string;
|
|
65
|
+
/** The field a node names: an identifier path the vocabulary knows. */
|
|
66
|
+
export declare function fieldRef(node: jsep.Expression, fields: readonly ConditionField[]): string | undefined;
|
|
67
|
+
/** Whether the vocabulary offers this operator and mode for the field. */
|
|
68
|
+
export declare function offered(c: Condition, vocabulary: ConditionVocabulary): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* One comparison as a condition row, or `undefined` when it is not one the editor can show: one it
|
|
71
|
+
* does not offer, or would flag, such as `name === ""`.
|
|
72
|
+
*/
|
|
73
|
+
export declare function parseCondition(node: jsep.Expression, vocabulary: ConditionVocabulary, id?: string): Condition | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* The rule, shown as stored, which RulesEngine cannot evaluate as its rows would show, marked with
|
|
76
|
+
* those rows and why. It shows as its expression, or preserved when no expression can name its
|
|
77
|
+
* fields.
|
|
78
|
+
*/
|
|
79
|
+
export declare function markRepairNeeded<R extends ExpressionRule | PreservedRule>(rule: R, conditions: ConditionGroup, reason: string): R;
|
|
80
|
+
/** The rule as its rows, if `markRepairNeeded` marked it and its text is still as stored. */
|
|
81
|
+
export declare function ruleNeedsRepair(rule: Rule): RuleRepair | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Converts between a rule's conditions and RulesEngine's expression language: strict comparisons
|
|
84
|
+
* joined by `&&` or `||`. Text matching, or an expression that mixes `&&` and `||` outside an
|
|
85
|
+
* option list, cannot cross over, so switching such a rule asks first. An expression RulesEngine
|
|
86
|
+
* cannot run is flagged on its rule. A stored rule RulesEngine cannot evaluate as its rows would
|
|
87
|
+
* show, such as a number stored as text, opens as stored, flagged with why, and its repair is those
|
|
88
|
+
* rows.
|
|
89
|
+
*/
|
|
90
|
+
export declare const rulesEngineDialect: RulesDialect;
|
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import jsep from "jsep";
|
|
2
|
+
import { DEFAULT_CONDITION_OPERATORS, createCondition, createConditionGroup } from "../ui/conditions-editor/index.js";
|
|
3
|
+
import { isComplete, operatorsFor, resolveCondition } from "../ui/conditions-editor/status.js";
|
|
4
|
+
import { withMode } from "../ui/rules-editor/model.js";
|
|
5
|
+
import { DEFAULT_FIELD_RULES_STRINGS } from "./field-rules-strings.js";
|
|
6
|
+
const FIXED_ONLY = [
|
|
7
|
+
'literal'
|
|
8
|
+
];
|
|
9
|
+
const FIXED_ONLY_OPERATORS = new Set([
|
|
10
|
+
'contains',
|
|
11
|
+
'startsWith',
|
|
12
|
+
'matches',
|
|
13
|
+
'isOneOf',
|
|
14
|
+
'isNotOneOf'
|
|
15
|
+
]);
|
|
16
|
+
const NATIVE_COMPARISONS = new Set([
|
|
17
|
+
'equals',
|
|
18
|
+
'notEquals'
|
|
19
|
+
]);
|
|
20
|
+
const COMPARISONS = new Set([
|
|
21
|
+
...NATIVE_COMPARISONS,
|
|
22
|
+
'greaterThan',
|
|
23
|
+
'lessThan'
|
|
24
|
+
]);
|
|
25
|
+
function createRulesEngineVocabulary({ operators = DEFAULT_CONDITION_OPERATORS, strings } = {}) {
|
|
26
|
+
const errorUnnamedField = strings?.errorUnnamedField ?? DEFAULT_FIELD_RULES_STRINGS.errorUnnamedField;
|
|
27
|
+
const unnamedFieldError = (operator)=>(_value, { field, mode })=>canName(field.value) || 'literal' === mode && NATIVE_COMPARISONS.has(operator) ? void 0 : errorUnnamedField;
|
|
28
|
+
const adapt = (options = [])=>options.map((option)=>FIXED_ONLY_OPERATORS.has(option.value) ? {
|
|
29
|
+
...option,
|
|
30
|
+
valueModes: FIXED_ONLY
|
|
31
|
+
} : COMPARISONS.has(option.value) ? {
|
|
32
|
+
...option,
|
|
33
|
+
valueError: unnamedFieldError(option.value)
|
|
34
|
+
} : option);
|
|
35
|
+
return {
|
|
36
|
+
operators: {
|
|
37
|
+
string: adapt(operators.string),
|
|
38
|
+
number: adapt(operators.number),
|
|
39
|
+
date: adapt(operators.date),
|
|
40
|
+
enum: adapt(operators.enum),
|
|
41
|
+
other: adapt(operators.other)
|
|
42
|
+
},
|
|
43
|
+
valueModes: [
|
|
44
|
+
'literal',
|
|
45
|
+
'expression'
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const rulesEngineVocabulary = createRulesEngineVocabulary();
|
|
50
|
+
const RULES_ENGINE_OPERATORS = rulesEngineVocabulary.operators;
|
|
51
|
+
function valueTypeOf(field) {
|
|
52
|
+
return field?.valueType ?? (field?.type === 'number' ? 'number' : 'string');
|
|
53
|
+
}
|
|
54
|
+
function typedValue(field, text) {
|
|
55
|
+
const type = valueTypeOf(field);
|
|
56
|
+
if ('number' === type) return Number(text);
|
|
57
|
+
if ('boolean' === type) return 'true' === text;
|
|
58
|
+
return text;
|
|
59
|
+
}
|
|
60
|
+
function valueText(value) {
|
|
61
|
+
return 'string' == typeof value ? value : JSON.stringify(value);
|
|
62
|
+
}
|
|
63
|
+
function printLiteral(value) {
|
|
64
|
+
return void 0 === value ? 'undefined' : JSON.stringify(value);
|
|
65
|
+
}
|
|
66
|
+
const IDENTIFIER_PATH = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/;
|
|
67
|
+
const LITERAL_NAMES = new Set([
|
|
68
|
+
'true',
|
|
69
|
+
'false',
|
|
70
|
+
'null',
|
|
71
|
+
'this',
|
|
72
|
+
'undefined'
|
|
73
|
+
]);
|
|
74
|
+
const GLOBAL_NAMES = new Set([
|
|
75
|
+
'undefined',
|
|
76
|
+
'NaN',
|
|
77
|
+
'Infinity'
|
|
78
|
+
]);
|
|
79
|
+
function canName(key) {
|
|
80
|
+
return IDENTIFIER_PATH.test(key) && !LITERAL_NAMES.has(key.split('.')[0]);
|
|
81
|
+
}
|
|
82
|
+
function printNode(node) {
|
|
83
|
+
const operand = (child)=>'BinaryExpression' === child.type || 'ConditionalExpression' === child.type ? `(${printNode(child)})` : printNode(child);
|
|
84
|
+
const target = (child)=>[
|
|
85
|
+
'Identifier',
|
|
86
|
+
'MemberExpression',
|
|
87
|
+
'CallExpression',
|
|
88
|
+
'ArrayExpression'
|
|
89
|
+
].includes(child.type) ? printNode(child) : `(${printNode(child)})`;
|
|
90
|
+
switch(node.type){
|
|
91
|
+
case 'Literal':
|
|
92
|
+
{
|
|
93
|
+
const literal = node;
|
|
94
|
+
return literal.raw ?? printLiteral(literal.value);
|
|
95
|
+
}
|
|
96
|
+
case 'Identifier':
|
|
97
|
+
return node.name;
|
|
98
|
+
case 'MemberExpression':
|
|
99
|
+
{
|
|
100
|
+
const member = node;
|
|
101
|
+
return member.computed ? `${target(member.object)}[${printNode(member.property)}]` : `${target(member.object)}.${printNode(member.property)}`;
|
|
102
|
+
}
|
|
103
|
+
case 'BinaryExpression':
|
|
104
|
+
{
|
|
105
|
+
const binary = node;
|
|
106
|
+
return `${operand(binary.left)} ${binary.operator} ${operand(binary.right)}`;
|
|
107
|
+
}
|
|
108
|
+
case 'UnaryExpression':
|
|
109
|
+
{
|
|
110
|
+
const unary = node;
|
|
111
|
+
return `${unary.operator}${operand(unary.argument)}`;
|
|
112
|
+
}
|
|
113
|
+
case 'ConditionalExpression':
|
|
114
|
+
{
|
|
115
|
+
const conditional = node;
|
|
116
|
+
return `${operand(conditional.test)} ? ${operand(conditional.consequent)} : ${operand(conditional.alternate)}`;
|
|
117
|
+
}
|
|
118
|
+
case 'CallExpression':
|
|
119
|
+
{
|
|
120
|
+
const call = node;
|
|
121
|
+
return `${target(call.callee)}(${call.arguments.map(printNode).join(', ')})`;
|
|
122
|
+
}
|
|
123
|
+
case 'ArrayExpression':
|
|
124
|
+
return `[${node.elements.map((element)=>element ? printNode(element) : '').join(', ')}]`;
|
|
125
|
+
default:
|
|
126
|
+
throw new Error(`Cannot print ${node.type}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function parseExpression(text) {
|
|
130
|
+
try {
|
|
131
|
+
return jsep(text);
|
|
132
|
+
} catch {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const COMPARISON = {
|
|
137
|
+
equals: '===',
|
|
138
|
+
notEquals: '!==',
|
|
139
|
+
greaterThan: '>',
|
|
140
|
+
lessThan: '<'
|
|
141
|
+
};
|
|
142
|
+
const emptyCheck = (name)=>`${name} == null || ${name} === ""`;
|
|
143
|
+
const notEmptyCheck = (name)=>`${name} != null && ${name} !== ""`;
|
|
144
|
+
function valueExpression(text) {
|
|
145
|
+
const node = parseExpression(text);
|
|
146
|
+
if (!node) return;
|
|
147
|
+
try {
|
|
148
|
+
return printNode(node);
|
|
149
|
+
} catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function conditionExpression(c, vocabulary) {
|
|
154
|
+
const resolved = resolveCondition(c, vocabulary);
|
|
155
|
+
const name = c.field ?? '';
|
|
156
|
+
if (!canName(name) || !resolved.operator) return;
|
|
157
|
+
const id = resolved.operator.value;
|
|
158
|
+
if ('isEmpty' === id || 'isNotEmpty' === id) {
|
|
159
|
+
if (resolved.field?.type === 'other') return;
|
|
160
|
+
return 'isEmpty' === id ? emptyCheck(name) : notEmptyCheck(name);
|
|
161
|
+
}
|
|
162
|
+
const symbol = COMPARISON[id];
|
|
163
|
+
if ('expression' === resolved.mode) {
|
|
164
|
+
const value = valueExpression(c.value ?? '');
|
|
165
|
+
return symbol && void 0 !== value ? `${name} ${symbol} (${value})` : void 0;
|
|
166
|
+
}
|
|
167
|
+
if ('literal' !== resolved.mode) return;
|
|
168
|
+
if (symbol) return `${name} ${symbol} ${printLiteral(typedValue(resolved.field, c.value ?? ''))}`;
|
|
169
|
+
if ('isOneOf' === id || 'isNotOneOf' === id) {
|
|
170
|
+
const values = (c.values ?? []).map((value)=>printLiteral(typedValue(resolved.field, value)));
|
|
171
|
+
const parts = values.map((value)=>`${name} ${'isOneOf' === id ? '===' : '!=='} ${value}`);
|
|
172
|
+
return parts.join('isOneOf' === id ? ' || ' : ' && ');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const isBlankCondition = (c)=>!c.field && !c.value && (c.values?.length ?? 0) === 0;
|
|
176
|
+
function conditionsExpression(group, vocabulary) {
|
|
177
|
+
if (0 === group.children.length) return 'true';
|
|
178
|
+
const parts = [];
|
|
179
|
+
for (const child of group.children){
|
|
180
|
+
if ('condition' === child.kind && isBlankCondition(child)) continue;
|
|
181
|
+
const text = 'condition' === child.kind && isComplete(child, vocabulary) ? conditionExpression(child, vocabulary) : void 0;
|
|
182
|
+
if (void 0 === text) return;
|
|
183
|
+
parts.push(text);
|
|
184
|
+
}
|
|
185
|
+
const wrap = (part)=>parts.length > 1 && /&&|\|\|/.test(part) ? `(${part})` : part;
|
|
186
|
+
return parts.map(wrap).join('all' === group.mode ? ' && ' : ' || ');
|
|
187
|
+
}
|
|
188
|
+
const BINARY_OPERATORS = new Set([
|
|
189
|
+
'==',
|
|
190
|
+
'===',
|
|
191
|
+
'!=',
|
|
192
|
+
'!==',
|
|
193
|
+
'<',
|
|
194
|
+
'>',
|
|
195
|
+
'<=',
|
|
196
|
+
'>=',
|
|
197
|
+
'&&',
|
|
198
|
+
'||',
|
|
199
|
+
'+',
|
|
200
|
+
'-',
|
|
201
|
+
'*',
|
|
202
|
+
'/',
|
|
203
|
+
'%'
|
|
204
|
+
]);
|
|
205
|
+
const UNARY_OPERATORS = new Set([
|
|
206
|
+
'!',
|
|
207
|
+
'-',
|
|
208
|
+
'+'
|
|
209
|
+
]);
|
|
210
|
+
function runs(node, fields) {
|
|
211
|
+
switch(node.type){
|
|
212
|
+
case 'Literal':
|
|
213
|
+
return true;
|
|
214
|
+
case 'Identifier':
|
|
215
|
+
{
|
|
216
|
+
const { name } = node;
|
|
217
|
+
return 'NaN' === name || 'Infinity' === name ? fields.some((field)=>field.value === name || field.value.startsWith(`${name}.`)) : true;
|
|
218
|
+
}
|
|
219
|
+
case 'MemberExpression':
|
|
220
|
+
{
|
|
221
|
+
const member = node;
|
|
222
|
+
return runs(member.object, fields) && (!member.computed || runs(member.property, fields));
|
|
223
|
+
}
|
|
224
|
+
case 'BinaryExpression':
|
|
225
|
+
{
|
|
226
|
+
const binary = node;
|
|
227
|
+
return BINARY_OPERATORS.has(binary.operator) && runs(binary.left, fields) && runs(binary.right, fields);
|
|
228
|
+
}
|
|
229
|
+
case 'UnaryExpression':
|
|
230
|
+
{
|
|
231
|
+
const unary = node;
|
|
232
|
+
return UNARY_OPERATORS.has(unary.operator) && runs(unary.argument, fields);
|
|
233
|
+
}
|
|
234
|
+
case 'ConditionalExpression':
|
|
235
|
+
{
|
|
236
|
+
const conditional = node;
|
|
237
|
+
return [
|
|
238
|
+
conditional.test,
|
|
239
|
+
conditional.consequent,
|
|
240
|
+
conditional.alternate
|
|
241
|
+
].every((part)=>runs(part, fields));
|
|
242
|
+
}
|
|
243
|
+
default:
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function canRun(expression, fields) {
|
|
248
|
+
const node = parseExpression(expression);
|
|
249
|
+
return void 0 !== node && runs(node, fields);
|
|
250
|
+
}
|
|
251
|
+
function pathOf(node) {
|
|
252
|
+
if ('Identifier' === node.type) return node.name;
|
|
253
|
+
const member = node;
|
|
254
|
+
if ('MemberExpression' !== member.type || member.computed) return;
|
|
255
|
+
const object = pathOf(member.object);
|
|
256
|
+
return void 0 === object ? void 0 : `${object}.${member.property.name}`;
|
|
257
|
+
}
|
|
258
|
+
function fieldKeysOf(expression) {
|
|
259
|
+
const root = parseExpression(expression);
|
|
260
|
+
const keys = new Set();
|
|
261
|
+
const visit = (node)=>{
|
|
262
|
+
const call = node;
|
|
263
|
+
if ('CallExpression' === call.type) {
|
|
264
|
+
const callee = call.callee;
|
|
265
|
+
const member = callee;
|
|
266
|
+
if ('MemberExpression' !== member.type || member.computed) {
|
|
267
|
+
if ('Identifier' !== callee.type) visit(callee);
|
|
268
|
+
} else visit(member.object);
|
|
269
|
+
for (const argument of call.arguments)visit(argument);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const key = pathOf(node);
|
|
273
|
+
if (void 0 !== key) {
|
|
274
|
+
if (!GLOBAL_NAMES.has(key)) keys.add(key);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const member = node;
|
|
278
|
+
for (const [name, value] of Object.entries(node))if ('MemberExpression' !== member.type || 'property' !== name || member.computed) {
|
|
279
|
+
for (const child of Array.isArray(value) ? value : [
|
|
280
|
+
value
|
|
281
|
+
])if (isNode(child)) visit(child);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
if (root) visit(root);
|
|
285
|
+
return Array.from(keys);
|
|
286
|
+
}
|
|
287
|
+
function renamedKey(key, from, to) {
|
|
288
|
+
if (key === from) return to;
|
|
289
|
+
return key.startsWith(`${from}.`) ? `${to}${key.slice(from.length)}` : void 0;
|
|
290
|
+
}
|
|
291
|
+
function renameInExpression(expression, from, to) {
|
|
292
|
+
const root = canName(to) ? parseExpression(expression) : void 0;
|
|
293
|
+
if (!root) return expression;
|
|
294
|
+
let changed = false;
|
|
295
|
+
const visit = (node)=>{
|
|
296
|
+
const call = node;
|
|
297
|
+
if ('CallExpression' === call.type) {
|
|
298
|
+
const callee = call.callee;
|
|
299
|
+
let next = call.callee;
|
|
300
|
+
if ('MemberExpression' !== callee.type || callee.computed) {
|
|
301
|
+
if ('Identifier' !== call.callee.type) next = visit(call.callee);
|
|
302
|
+
} else next = {
|
|
303
|
+
...callee,
|
|
304
|
+
object: visit(callee.object)
|
|
305
|
+
};
|
|
306
|
+
return {
|
|
307
|
+
...call,
|
|
308
|
+
callee: next,
|
|
309
|
+
arguments: call.arguments.map(visit)
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
const path = pathOf(node);
|
|
313
|
+
const renamed = void 0 === path ? void 0 : renamedKey(path, from, to);
|
|
314
|
+
if (void 0 !== renamed) {
|
|
315
|
+
const replacement = parseExpression(renamed);
|
|
316
|
+
if (replacement) {
|
|
317
|
+
changed = true;
|
|
318
|
+
return replacement;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const member = node;
|
|
322
|
+
const next = {
|
|
323
|
+
...node
|
|
324
|
+
};
|
|
325
|
+
for (const [name, value] of Object.entries(node))if ('MemberExpression' !== node.type || 'property' !== name || member.computed) {
|
|
326
|
+
if (Array.isArray(value)) next[name] = value.map((item)=>isNode(item) ? visit(item) : item);
|
|
327
|
+
else if (isNode(value)) next[name] = visit(value);
|
|
328
|
+
}
|
|
329
|
+
return next;
|
|
330
|
+
};
|
|
331
|
+
const result = visit(root);
|
|
332
|
+
if (!changed) return expression;
|
|
333
|
+
try {
|
|
334
|
+
return printNode(result);
|
|
335
|
+
} catch {
|
|
336
|
+
return expression;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const isNode = (value)=>!!value && 'object' == typeof value && 'type' in value;
|
|
340
|
+
function fieldRef(node, fields) {
|
|
341
|
+
let key;
|
|
342
|
+
if ('Identifier' === node.type) key = node.name;
|
|
343
|
+
else if ('MemberExpression' === node.type) {
|
|
344
|
+
try {
|
|
345
|
+
key = printNode(node);
|
|
346
|
+
} catch {
|
|
347
|
+
key = void 0;
|
|
348
|
+
}
|
|
349
|
+
if (key && !canName(key)) key = void 0;
|
|
350
|
+
}
|
|
351
|
+
return void 0 !== key && fields.some((field)=>field.value === key) ? key : void 0;
|
|
352
|
+
}
|
|
353
|
+
function literalValue(node) {
|
|
354
|
+
if ('Literal' === node.type) return {
|
|
355
|
+
value: node.value
|
|
356
|
+
};
|
|
357
|
+
const unary = node;
|
|
358
|
+
if ('UnaryExpression' === unary.type && '-' === unary.operator && 'Literal' === unary.argument.type) {
|
|
359
|
+
const value = unary.argument.value;
|
|
360
|
+
return 'number' == typeof value ? {
|
|
361
|
+
value: -value
|
|
362
|
+
} : void 0;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const OPERATOR_FOR = {
|
|
366
|
+
'===': 'equals',
|
|
367
|
+
'!==': 'notEquals',
|
|
368
|
+
'>': 'greaterThan',
|
|
369
|
+
'<': 'lessThan'
|
|
370
|
+
};
|
|
371
|
+
function isFieldTyped(value, key, fields) {
|
|
372
|
+
const field = fields.find((option)=>option.value === key);
|
|
373
|
+
return typeof value === valueTypeOf(field);
|
|
374
|
+
}
|
|
375
|
+
function isNullLiteral(node) {
|
|
376
|
+
return 'Literal' === node.type && null === node.value;
|
|
377
|
+
}
|
|
378
|
+
function isEmptyStringLiteral(node) {
|
|
379
|
+
return 'Literal' === node.type && '' === node.value;
|
|
380
|
+
}
|
|
381
|
+
function emptiness(node, fields) {
|
|
382
|
+
const left = node.left;
|
|
383
|
+
const right = node.right;
|
|
384
|
+
if ('BinaryExpression' !== left.type || 'BinaryExpression' !== right.type) return;
|
|
385
|
+
const field = fieldRef(left.left, fields);
|
|
386
|
+
if (!field || !isNullLiteral(left.right)) return;
|
|
387
|
+
if (fields.find((option)=>option.value === field)?.type === 'other') return;
|
|
388
|
+
if (fieldRef(right.left, fields) !== field || !isEmptyStringLiteral(right.right)) return;
|
|
389
|
+
if ('||' === node.operator && '==' === left.operator && '===' === right.operator) return {
|
|
390
|
+
field,
|
|
391
|
+
operator: 'isEmpty'
|
|
392
|
+
};
|
|
393
|
+
if ('&&' === node.operator && '!=' === left.operator && '!==' === right.operator) return {
|
|
394
|
+
field,
|
|
395
|
+
operator: 'isNotEmpty'
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function offered(c, vocabulary) {
|
|
399
|
+
const field = vocabulary.fields.find((option)=>option.value === c.field);
|
|
400
|
+
const op = operatorsFor(field, vocabulary.operators).find((option)=>option.value === c.operator);
|
|
401
|
+
if (!op) return false;
|
|
402
|
+
const mode = c.valueMode ?? 'literal';
|
|
403
|
+
if (!vocabulary.valueModes.includes(mode)) return false;
|
|
404
|
+
return !op.valueModes || op.valueModes.includes(mode);
|
|
405
|
+
}
|
|
406
|
+
function parseCondition(node, vocabulary, id) {
|
|
407
|
+
let row;
|
|
408
|
+
const binary = node;
|
|
409
|
+
if ('BinaryExpression' !== node.type) return;
|
|
410
|
+
if ('||' === binary.operator || '&&' === binary.operator) row = emptiness(binary, vocabulary.fields);
|
|
411
|
+
else {
|
|
412
|
+
const field = fieldRef(binary.left, vocabulary.fields);
|
|
413
|
+
const op = OPERATOR_FOR[binary.operator];
|
|
414
|
+
if (!field || !op) return;
|
|
415
|
+
const literal = literalValue(binary.right);
|
|
416
|
+
if (literal && isFieldTyped(literal.value, field, vocabulary.fields)) row = {
|
|
417
|
+
field,
|
|
418
|
+
operator: op,
|
|
419
|
+
value: valueText(literal.value)
|
|
420
|
+
};
|
|
421
|
+
else if (!literal) {
|
|
422
|
+
let text;
|
|
423
|
+
try {
|
|
424
|
+
text = printNode(binary.right);
|
|
425
|
+
} catch {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
row = {
|
|
429
|
+
field,
|
|
430
|
+
operator: op,
|
|
431
|
+
valueMode: 'expression',
|
|
432
|
+
value: text
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (!row) return;
|
|
437
|
+
const condition = createCondition({
|
|
438
|
+
...row,
|
|
439
|
+
...id ? {
|
|
440
|
+
id
|
|
441
|
+
} : {}
|
|
442
|
+
});
|
|
443
|
+
return offered(condition, vocabulary) && isComplete(condition, vocabulary) ? condition : void 0;
|
|
444
|
+
}
|
|
445
|
+
function chainParts(node, fields) {
|
|
446
|
+
const parts = [];
|
|
447
|
+
const collect = (part)=>{
|
|
448
|
+
const inner = part;
|
|
449
|
+
if ('BinaryExpression' !== inner.type || inner.operator !== node.operator || emptiness(inner, fields)) parts.push(part);
|
|
450
|
+
else {
|
|
451
|
+
collect(inner.left);
|
|
452
|
+
collect(inner.right);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
collect(node);
|
|
456
|
+
return parts;
|
|
457
|
+
}
|
|
458
|
+
function parseList(node, vocabulary) {
|
|
459
|
+
const binary = node;
|
|
460
|
+
if ('BinaryExpression' !== binary.type) return;
|
|
461
|
+
const list = '||' === binary.operator ? {
|
|
462
|
+
operator: 'isOneOf',
|
|
463
|
+
compare: '==='
|
|
464
|
+
} : '&&' === binary.operator ? {
|
|
465
|
+
operator: 'isNotOneOf',
|
|
466
|
+
compare: '!=='
|
|
467
|
+
} : void 0;
|
|
468
|
+
if (!list) return;
|
|
469
|
+
let field;
|
|
470
|
+
const values = [];
|
|
471
|
+
for (const part of chainParts(binary, vocabulary.fields)){
|
|
472
|
+
const comparison = part;
|
|
473
|
+
if ('BinaryExpression' !== comparison.type || comparison.operator !== list.compare) return;
|
|
474
|
+
const name = fieldRef(comparison.left, vocabulary.fields);
|
|
475
|
+
const literal = literalValue(comparison.right);
|
|
476
|
+
if (!name || field && name !== field || !literal) return;
|
|
477
|
+
if (!isFieldTyped(literal.value, name, vocabulary.fields)) return;
|
|
478
|
+
field = name;
|
|
479
|
+
values.push(valueText(literal.value));
|
|
480
|
+
}
|
|
481
|
+
const condition = createCondition({
|
|
482
|
+
field,
|
|
483
|
+
operator: list.operator,
|
|
484
|
+
values
|
|
485
|
+
});
|
|
486
|
+
return offered(condition, vocabulary) && isComplete(condition, vocabulary) ? condition : void 0;
|
|
487
|
+
}
|
|
488
|
+
function parseGroup(node, vocabulary) {
|
|
489
|
+
if ('Literal' === node.type && true === node.value) return createConditionGroup();
|
|
490
|
+
const single = parseCondition(node, vocabulary) ?? parseList(node, vocabulary);
|
|
491
|
+
if (single) return createConditionGroup('all', [
|
|
492
|
+
single
|
|
493
|
+
]);
|
|
494
|
+
const binary = node;
|
|
495
|
+
if ('BinaryExpression' !== binary.type || '&&' !== binary.operator && '||' !== binary.operator) return;
|
|
496
|
+
const children = [];
|
|
497
|
+
for (const part of chainParts(binary, vocabulary.fields)){
|
|
498
|
+
const condition = parseCondition(part, vocabulary) ?? parseList(part, vocabulary);
|
|
499
|
+
if (!condition) return;
|
|
500
|
+
children.push(condition);
|
|
501
|
+
}
|
|
502
|
+
return createConditionGroup('&&' === binary.operator ? 'all' : 'any', children);
|
|
503
|
+
}
|
|
504
|
+
const sourceOf = (rule)=>'expression' === rule.mode ? rule.expression : 'preserved' === rule.mode ? rule.preservedSource : void 0;
|
|
505
|
+
function markRepairNeeded(rule, conditions, reason) {
|
|
506
|
+
return {
|
|
507
|
+
...rule,
|
|
508
|
+
storedRepair: {
|
|
509
|
+
source: sourceOf(rule) ?? '',
|
|
510
|
+
conditions,
|
|
511
|
+
reason
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function ruleNeedsRepair(rule) {
|
|
516
|
+
if ('conditions' === rule.mode) return;
|
|
517
|
+
const { storedRepair, ...rest } = rule;
|
|
518
|
+
if (!storedRepair || storedRepair.source !== sourceOf(rule)) return;
|
|
519
|
+
return {
|
|
520
|
+
rule: withMode(rest, {
|
|
521
|
+
mode: 'conditions',
|
|
522
|
+
conditions: storedRepair.conditions
|
|
523
|
+
}),
|
|
524
|
+
reason: storedRepair.reason
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
const rulesEngineDialect = {
|
|
528
|
+
toExpression: (conditions, vocabulary)=>conditionsExpression(conditions, vocabulary),
|
|
529
|
+
fieldsOf: fieldKeysOf,
|
|
530
|
+
fromExpression: (expression, vocabulary)=>{
|
|
531
|
+
const node = parseExpression(expression);
|
|
532
|
+
return node ? parseGroup(node, vocabulary) : void 0;
|
|
533
|
+
},
|
|
534
|
+
checkExpression: (rule, vocabulary)=>canRun(rule.expression, vocabulary.fields) || ruleNeedsRepair(rule) ? void 0 : 'invalid',
|
|
535
|
+
repair: (rule)=>ruleNeedsRepair(rule)
|
|
536
|
+
};
|
|
537
|
+
export { RULES_ENGINE_OPERATORS, canName, canRun, conditionExpression, createRulesEngineVocabulary, emptyCheck, fieldKeysOf, fieldRef, markRepairNeeded, notEmptyCheck, offered, parseCondition, parseExpression, printLiteral, printNode, renameInExpression, ruleNeedsRepair, rulesEngineDialect, rulesEngineVocabulary, typedValue, valueText, valueTypeOf };
|