@valerypopoff/rivet2-core 2.11.0 → 2.12.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/cjs/bundle.cjs +676 -469
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/model/Nodes.js +3 -0
- package/dist/esm/model/nodes/CoalesceNewNode.js +1 -1
- package/dist/esm/model/nodes/CoalesceNode.js +1 -1
- package/dist/esm/model/nodes/MatchCaseNode.js +132 -0
- package/dist/esm/model/nodes/MatchCaseNodeBase.js +237 -0
- package/dist/esm/model/nodes/MatchNode.js +24 -177
- package/dist/types/model/EditorDefinition.d.ts +2 -0
- package/dist/types/model/Nodes.d.ts +3 -2
- package/dist/types/model/nodes/MatchCaseNode.d.ts +43 -0
- package/dist/types/model/nodes/MatchCaseNodeBase.d.ts +52 -0
- package/dist/types/model/nodes/MatchNode.d.ts +13 -21
- package/package.json +1 -1
package/dist/esm/model/Nodes.js
CHANGED
|
@@ -22,6 +22,8 @@ import { expressionNode } from './nodes/ExpressionNode.js';
|
|
|
22
22
|
export * from './nodes/ExpressionNode.js';
|
|
23
23
|
import { matchNode } from './nodes/MatchNode.js';
|
|
24
24
|
export * from './nodes/MatchNode.js';
|
|
25
|
+
import { matchCaseNode } from './nodes/MatchCaseNode.js';
|
|
26
|
+
export * from './nodes/MatchCaseNode.js';
|
|
25
27
|
import { ifNode } from './nodes/IfNode.js';
|
|
26
28
|
export * from './nodes/IfNode.js';
|
|
27
29
|
import { readDirectoryNode } from './nodes/ReadDirectoryNode.js';
|
|
@@ -227,6 +229,7 @@ export const registerBuiltInNodes = (registry) => {
|
|
|
227
229
|
.register(codeNewNode)
|
|
228
230
|
.register(expressionNode)
|
|
229
231
|
.register(matchNode)
|
|
232
|
+
.register(matchCaseNode)
|
|
230
233
|
.register(ifNode)
|
|
231
234
|
.register(readDirectoryNode)
|
|
232
235
|
.register(readFileNode)
|
|
@@ -9,7 +9,7 @@ export class CoalesceNewNodeImpl extends CoalesceNodeBase {
|
|
|
9
9
|
static getUIData() {
|
|
10
10
|
return {
|
|
11
11
|
infoBoxBody: dedent `
|
|
12
|
-
Takes in any number of inputs and outputs the first value that is not "Not Ran". Useful for consolidating branches after a Match node.
|
|
12
|
+
Takes in any number of inputs and outputs the first value that is not "Not Ran". Useful for consolidating branches after a Match case node.
|
|
13
13
|
|
|
14
14
|
Null and undefined input values are emitted by default, but the node can be configured to skip either value and continue checking later inputs.
|
|
15
15
|
`,
|
|
@@ -12,7 +12,7 @@ export class CoalesceNodeImpl extends CoalesceNodeBase {
|
|
|
12
12
|
static getUIData() {
|
|
13
13
|
return {
|
|
14
14
|
infoBoxBody: dedent `
|
|
15
|
-
Legacy Coalesce node. Takes in any number of inputs and outputs the first value that is not "Not Ran". Useful for consolidating branches after a Match node.
|
|
15
|
+
Legacy Coalesce node. Takes in any number of inputs and outputs the first value that is not "Not Ran". Useful for consolidating branches after a Match case node.
|
|
16
16
|
|
|
17
17
|
Its Conditional input can exclude the node itself. New graphs should use Coalesce instead, which has the same fallback behavior without that port.
|
|
18
18
|
`,
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { dedent } from 'ts-dedent';
|
|
3
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
4
|
+
import {} from '../NodeImpl.js';
|
|
5
|
+
import {} from '../NodeBase.js';
|
|
6
|
+
import {} from '../EditorDefinition.js';
|
|
7
|
+
import { MatchCaseNodeBase, } from './MatchCaseNodeBase.js';
|
|
8
|
+
/** Match case is the current routing node; legacy `match` remains regex-only. */
|
|
9
|
+
export class MatchCaseNodeImpl extends MatchCaseNodeBase {
|
|
10
|
+
caseEditorLabel = 'Cases';
|
|
11
|
+
caseEditorPlaceholder = 'Case';
|
|
12
|
+
casesEditorPlacement = 'before-trigger';
|
|
13
|
+
customCaseValuesEditorLabel = '';
|
|
14
|
+
defaultExclusive = true;
|
|
15
|
+
firstMatchingBodyValue = 'First matching case';
|
|
16
|
+
inputTitle = 'Input';
|
|
17
|
+
matchingCasesToTriggerOptions = [
|
|
18
|
+
{ value: true, label: 'First matching case' },
|
|
19
|
+
{ value: false, label: 'All matching cases' },
|
|
20
|
+
];
|
|
21
|
+
sharedCustomValueInputTitle = 'Return value';
|
|
22
|
+
triggerEditorLabel = 'Trigger';
|
|
23
|
+
static create() {
|
|
24
|
+
return {
|
|
25
|
+
type: 'matchCase',
|
|
26
|
+
title: 'Match case',
|
|
27
|
+
id: nanoid(),
|
|
28
|
+
visualData: {
|
|
29
|
+
x: 0,
|
|
30
|
+
y: 0,
|
|
31
|
+
width: 250,
|
|
32
|
+
},
|
|
33
|
+
data: {
|
|
34
|
+
cases: ['YES', 'NO'],
|
|
35
|
+
casePortIds: [nanoid(), nanoid()],
|
|
36
|
+
valueInputMode: 'shared',
|
|
37
|
+
matchMode: 'plainText',
|
|
38
|
+
caseSensitive: true,
|
|
39
|
+
exclusive: true,
|
|
40
|
+
returnValue: 'true',
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
matchesCase(input, caseValue) {
|
|
45
|
+
if (this.getMatchMode() === 'regex') {
|
|
46
|
+
return new RegExp(caseValue).test(input);
|
|
47
|
+
}
|
|
48
|
+
if (this.getCaseSensitive()) {
|
|
49
|
+
return input === caseValue;
|
|
50
|
+
}
|
|
51
|
+
return input.toLowerCase() === caseValue.toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
describeCaseMatch(caseValue) {
|
|
54
|
+
return this.getMatchMode() === 'plainText'
|
|
55
|
+
? this.getCaseSensitive()
|
|
56
|
+
? `the Input value exactly equals ${JSON.stringify(caseValue)}`
|
|
57
|
+
: `the Input value equals ${JSON.stringify(caseValue)} without regard to case`
|
|
58
|
+
: `/${caseValue}/ matches the Input value`;
|
|
59
|
+
}
|
|
60
|
+
describeNoCaseMatches() {
|
|
61
|
+
if (this.getMatchMode() === 'regex') {
|
|
62
|
+
return 'no regular expression matches the Input value';
|
|
63
|
+
}
|
|
64
|
+
return this.getCaseSensitive()
|
|
65
|
+
? 'no case exactly equals the Input value'
|
|
66
|
+
: 'no case equals the Input value without regard to case';
|
|
67
|
+
}
|
|
68
|
+
getReturnValueMode() {
|
|
69
|
+
return this.data.returnValue === 'testValue' || this.data.returnValue === 'custom'
|
|
70
|
+
? this.data.returnValue
|
|
71
|
+
: 'true';
|
|
72
|
+
}
|
|
73
|
+
getEditorsAfterTrigger() {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
type: 'segmented',
|
|
77
|
+
label: 'Output value',
|
|
78
|
+
dataKey: 'returnValue',
|
|
79
|
+
defaultValue: 'true',
|
|
80
|
+
options: [
|
|
81
|
+
{ value: 'true', label: 'True' },
|
|
82
|
+
{ value: 'testValue', label: 'Input value' },
|
|
83
|
+
{ value: 'custom', label: 'Custom' },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
shouldHideCustomCaseValues(data) {
|
|
89
|
+
return (data.returnValue ?? 'true') !== 'custom';
|
|
90
|
+
}
|
|
91
|
+
getEditors() {
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
type: 'segmented',
|
|
95
|
+
label: 'Match mode',
|
|
96
|
+
dataKey: 'matchMode',
|
|
97
|
+
defaultValue: 'plainText',
|
|
98
|
+
options: [
|
|
99
|
+
{ value: 'plainText', label: 'Plain text' },
|
|
100
|
+
{ value: 'regex', label: 'Regular expression' },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
type: 'toggle',
|
|
105
|
+
label: 'Case sensitive',
|
|
106
|
+
dataKey: 'caseSensitive',
|
|
107
|
+
defaultValue: true,
|
|
108
|
+
hideIf: (data) => data.matchMode === 'regex',
|
|
109
|
+
},
|
|
110
|
+
...super.getEditors(),
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
static getUIData() {
|
|
114
|
+
return {
|
|
115
|
+
infoBoxBody: dedent `
|
|
116
|
+
Routes an Input value to matching case outputs. Plain text cases use exact, case-sensitive equality by default; turn off Case sensitive to ignore letter case. Choose Regular expression to use the existing JavaScript RegExp pattern behavior; the Case sensitive switch does not apply in that mode.
|
|
117
|
+
|
|
118
|
+
Choose First matching case to trigger only the first matching output, or All matching cases to trigger every matching output. Active outputs return True by default. They can instead return the Input value, or a custom value supplied through one shared Return value input or separate Return values inputs for every case and Unmatched.
|
|
119
|
+
`,
|
|
120
|
+
infoBoxTitle: 'Match case Node',
|
|
121
|
+
contextMenuTitle: 'Match case',
|
|
122
|
+
group: ['Logic'],
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
getMatchMode() {
|
|
126
|
+
return this.data.matchMode === 'regex' ? 'regex' : 'plainText';
|
|
127
|
+
}
|
|
128
|
+
getCaseSensitive() {
|
|
129
|
+
return this.data.caseSensitive !== false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export const matchCaseNode = nodeDefinition(MatchCaseNodeImpl, 'Match case');
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import {} from '../DataValue.js';
|
|
2
|
+
import {} from '../EditorDefinition.js';
|
|
3
|
+
import {} from '../GraphProcessor.js';
|
|
4
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
5
|
+
import {} from '../NodeBase.js';
|
|
6
|
+
import { coerceType } from '../../utils/coerceType.js';
|
|
7
|
+
import { resolveStoredOrderedPortIds } from '../../utils/orderedStringPortIds.js';
|
|
8
|
+
/**
|
|
9
|
+
* Shared ports, custom-value routing, and case-output handling for the current
|
|
10
|
+
* Match case node and the regex-only compatibility node. Each subclass owns
|
|
11
|
+
* only its matching semantics and editor wording.
|
|
12
|
+
*/
|
|
13
|
+
export class MatchCaseNodeBase extends NodeImpl {
|
|
14
|
+
casesEditorPlacement = 'after-custom-values';
|
|
15
|
+
customCaseValuesEditorLabel = 'Custom case values';
|
|
16
|
+
defaultExclusive = false;
|
|
17
|
+
firstMatchingBodyValue = 'First matching case only';
|
|
18
|
+
inputTitle = 'Test';
|
|
19
|
+
matchingCasesToTriggerOptions = [
|
|
20
|
+
{ value: false, label: 'Trigger all matching cases' },
|
|
21
|
+
{ value: true, label: 'Trigger first only' },
|
|
22
|
+
];
|
|
23
|
+
sharedCustomValueInputTitle = 'Custom value';
|
|
24
|
+
triggerEditorLabel = 'Matching cases to trigger';
|
|
25
|
+
getReturnValueMode() {
|
|
26
|
+
return 'custom';
|
|
27
|
+
}
|
|
28
|
+
getEditorsAfterTrigger() {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
shouldHideCustomCaseValues(_data) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
getInputDefinitions() {
|
|
35
|
+
const inputs = [
|
|
36
|
+
{
|
|
37
|
+
id: 'input',
|
|
38
|
+
title: this.inputTitle,
|
|
39
|
+
dataType: 'string',
|
|
40
|
+
required: true,
|
|
41
|
+
description: `The ${this.inputTitle} value tested against each case.`,
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
if (this.getReturnValueMode() !== 'custom') {
|
|
45
|
+
return inputs;
|
|
46
|
+
}
|
|
47
|
+
if (this.getValueInputMode() === 'shared') {
|
|
48
|
+
inputs.push({
|
|
49
|
+
id: 'value',
|
|
50
|
+
title: this.sharedCustomValueInputTitle,
|
|
51
|
+
dataType: 'any',
|
|
52
|
+
description: `The optional value passed through to every matching output. If unconnected, the ${this.inputTitle} value is passed through.`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const portIds = this.getCasePortIds();
|
|
57
|
+
this.data.cases.forEach((caseValue, index) => {
|
|
58
|
+
inputs.push({
|
|
59
|
+
id: this.getCaseValueInputId(portIds[index]),
|
|
60
|
+
title: this.getCaseTitle(caseValue, index),
|
|
61
|
+
dataType: 'any',
|
|
62
|
+
description: `The optional custom value emitted when ${this.describeCaseMatch(caseValue)}. If unconnected, ${this.inputTitle} is passed through.`,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
inputs.push({
|
|
66
|
+
id: 'value-unmatched',
|
|
67
|
+
title: 'Unmatched',
|
|
68
|
+
dataType: 'any',
|
|
69
|
+
description: `The optional custom value emitted when ${this.describeNoCaseMatches()}. If unconnected, ${this.inputTitle} is passed through.`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return inputs;
|
|
73
|
+
}
|
|
74
|
+
getOutputDefinitions() {
|
|
75
|
+
const outputs = [];
|
|
76
|
+
const portIds = this.getCasePortIds();
|
|
77
|
+
const returnValueMode = this.getReturnValueMode();
|
|
78
|
+
const dataType = returnValueMode === 'true' ? 'boolean' : returnValueMode === 'testValue' ? 'string' : 'any';
|
|
79
|
+
for (let index = 0; index < this.data.cases.length; index++) {
|
|
80
|
+
const caseValue = this.data.cases[index];
|
|
81
|
+
const title = this.getCaseTitle(caseValue, index);
|
|
82
|
+
outputs.push({
|
|
83
|
+
id: portIds[index],
|
|
84
|
+
title,
|
|
85
|
+
dataType,
|
|
86
|
+
description: this.getMatchingOutputDescription(caseValue, title),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
outputs.push({
|
|
90
|
+
id: 'unmatched',
|
|
91
|
+
title: 'Unmatched',
|
|
92
|
+
dataType,
|
|
93
|
+
description: this.getUnmatchedOutputDescription(),
|
|
94
|
+
});
|
|
95
|
+
return outputs;
|
|
96
|
+
}
|
|
97
|
+
getBody() {
|
|
98
|
+
return this.getExclusive() ? `Trigger: ${this.firstMatchingBodyValue}` : 'Trigger: All matching cases';
|
|
99
|
+
}
|
|
100
|
+
getEditors() {
|
|
101
|
+
const casesEditor = {
|
|
102
|
+
type: 'stringList',
|
|
103
|
+
dataKey: 'cases',
|
|
104
|
+
label: this.caseEditorLabel,
|
|
105
|
+
placeholder: this.caseEditorPlaceholder,
|
|
106
|
+
reorderable: true,
|
|
107
|
+
portBinding: {
|
|
108
|
+
side: 'output',
|
|
109
|
+
identity: 'stored-stable-id',
|
|
110
|
+
idDataKey: 'casePortIds',
|
|
111
|
+
legacyPortIdPattern: {
|
|
112
|
+
kind: 'prefix',
|
|
113
|
+
prefix: 'case',
|
|
114
|
+
startIndex: 1,
|
|
115
|
+
},
|
|
116
|
+
companionBindings: [{ side: 'input', prefix: 'value-' }],
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
return [
|
|
120
|
+
...(this.casesEditorPlacement === 'before-trigger' ? [casesEditor] : []),
|
|
121
|
+
{
|
|
122
|
+
type: 'segmented',
|
|
123
|
+
label: this.triggerEditorLabel,
|
|
124
|
+
dataKey: 'exclusive',
|
|
125
|
+
defaultValue: this.defaultExclusive,
|
|
126
|
+
options: this.matchingCasesToTriggerOptions,
|
|
127
|
+
},
|
|
128
|
+
...this.getEditorsAfterTrigger(),
|
|
129
|
+
{
|
|
130
|
+
type: 'segmented',
|
|
131
|
+
label: this.customCaseValuesEditorLabel,
|
|
132
|
+
ariaLabel: 'Custom case values',
|
|
133
|
+
dataKey: 'valueInputMode',
|
|
134
|
+
defaultValue: 'shared',
|
|
135
|
+
hideIf: (data) => this.shouldHideCustomCaseValues(data),
|
|
136
|
+
options: [
|
|
137
|
+
{ value: 'shared', label: 'One shared custom value' },
|
|
138
|
+
{ value: 'per-output', label: 'Custom values per case' },
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
...(this.casesEditorPlacement === 'after-custom-values' ? [casesEditor] : []),
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
async process(inputs) {
|
|
145
|
+
const inputValue = inputs['input'];
|
|
146
|
+
const inputString = inputValue?.value == null ? undefined : coerceType(inputValue, 'string');
|
|
147
|
+
const portIds = this.getCasePortIds();
|
|
148
|
+
const sharedValue = inputs['value'];
|
|
149
|
+
const returnValueMode = this.getReturnValueMode();
|
|
150
|
+
const getInputOutputValue = () => ({
|
|
151
|
+
type: 'string',
|
|
152
|
+
value: inputString,
|
|
153
|
+
});
|
|
154
|
+
const getSharedOutputValue = () => sharedValue ?? getInputOutputValue();
|
|
155
|
+
const getConfiguredOutputValue = (customValueInputId) => {
|
|
156
|
+
if (returnValueMode === 'true') {
|
|
157
|
+
return { type: 'boolean', value: true };
|
|
158
|
+
}
|
|
159
|
+
if (returnValueMode === 'testValue') {
|
|
160
|
+
return getInputOutputValue();
|
|
161
|
+
}
|
|
162
|
+
return this.getValueInputMode() === 'shared'
|
|
163
|
+
? getSharedOutputValue()
|
|
164
|
+
: inputs[customValueInputId] ?? getInputOutputValue();
|
|
165
|
+
};
|
|
166
|
+
const getOutputValue = (portId) => getConfiguredOutputValue(this.getCaseValueInputId(portId));
|
|
167
|
+
const getUnmatchedOutputValue = () => getConfiguredOutputValue('value-unmatched');
|
|
168
|
+
let matched = false;
|
|
169
|
+
const output = {};
|
|
170
|
+
for (let index = 0; index < this.data.cases.length; index++) {
|
|
171
|
+
const caseValue = this.data.cases[index];
|
|
172
|
+
const matches = inputString !== undefined && this.matchesCase(inputString, caseValue);
|
|
173
|
+
const canMatch = !this.getExclusive() || !matched;
|
|
174
|
+
const portId = portIds[index];
|
|
175
|
+
if (matches && canMatch) {
|
|
176
|
+
matched = true;
|
|
177
|
+
output[portId] = getOutputValue(portId);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
output[portId] = {
|
|
181
|
+
type: 'control-flow-excluded',
|
|
182
|
+
value: undefined,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
output['unmatched'] = matched
|
|
187
|
+
? {
|
|
188
|
+
type: 'control-flow-excluded',
|
|
189
|
+
value: undefined,
|
|
190
|
+
}
|
|
191
|
+
: getUnmatchedOutputValue();
|
|
192
|
+
return output;
|
|
193
|
+
}
|
|
194
|
+
getValueInputMode() {
|
|
195
|
+
return this.data.valueInputMode === 'per-output' ? 'per-output' : 'shared';
|
|
196
|
+
}
|
|
197
|
+
getExclusive() {
|
|
198
|
+
return this.data.exclusive ?? this.defaultExclusive;
|
|
199
|
+
}
|
|
200
|
+
getMatchingOutputDescription(caseValue, title) {
|
|
201
|
+
const condition = this.describeCaseMatch(caseValue);
|
|
202
|
+
if (this.getReturnValueMode() === 'true') {
|
|
203
|
+
return `True if ${condition}.`;
|
|
204
|
+
}
|
|
205
|
+
if (this.getReturnValueMode() === 'testValue') {
|
|
206
|
+
return `The ${this.inputTitle} value passed through if ${condition}.`;
|
|
207
|
+
}
|
|
208
|
+
return this.getValueInputMode() === 'shared'
|
|
209
|
+
? `The shared ${this.sharedCustomValueInputTitle} (or ${this.inputTitle} if it is unconnected) passed through if ${condition}.`
|
|
210
|
+
: `The corresponding ${title} custom value (or ${this.inputTitle} if it is unconnected) passed through if ${condition}.`;
|
|
211
|
+
}
|
|
212
|
+
getUnmatchedOutputDescription() {
|
|
213
|
+
const condition = this.describeNoCaseMatches();
|
|
214
|
+
if (this.getReturnValueMode() === 'true') {
|
|
215
|
+
return `True if ${condition}.`;
|
|
216
|
+
}
|
|
217
|
+
if (this.getReturnValueMode() === 'testValue') {
|
|
218
|
+
return `The ${this.inputTitle} value passed through if ${condition}.`;
|
|
219
|
+
}
|
|
220
|
+
return this.getValueInputMode() === 'shared'
|
|
221
|
+
? `The shared ${this.sharedCustomValueInputTitle} (or ${this.inputTitle} if it is unconnected) passed through if ${condition}.`
|
|
222
|
+
: `The Unmatched custom value (or ${this.inputTitle} if it is unconnected) passed through if ${condition}.`;
|
|
223
|
+
}
|
|
224
|
+
getCasePortIds() {
|
|
225
|
+
return resolveStoredOrderedPortIds(this.data.cases.length, this.data.casePortIds, {
|
|
226
|
+
kind: 'prefix',
|
|
227
|
+
prefix: 'case',
|
|
228
|
+
startIndex: 1,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
getCaseTitle(caseValue, index) {
|
|
232
|
+
return caseValue?.trim() ? caseValue : `Case ${index + 1}`;
|
|
233
|
+
}
|
|
234
|
+
getCaseValueInputId(portId) {
|
|
235
|
+
return `value-${portId}`;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import {} from '../NodeBase.js';
|
|
2
1
|
import { nanoid } from 'nanoid/non-secure';
|
|
3
|
-
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
-
import { nodeDefinition } from '../NodeDefinition.js';
|
|
5
|
-
import {} from '../DataValue.js';
|
|
6
|
-
import {} from '../../index.js';
|
|
7
2
|
import { dedent } from 'ts-dedent';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
3
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
4
|
+
import {} from '../NodeImpl.js';
|
|
5
|
+
import {} from '../NodeBase.js';
|
|
6
|
+
import { MatchCaseNodeBase } from './MatchCaseNodeBase.js';
|
|
7
|
+
/** Display-only label; the persisted `match` type remains available for existing graphs. */
|
|
8
|
+
export const regexMatchLegacyDisplayName = 'Regex Match (legacy)';
|
|
9
|
+
/** The persisted `match` type stays regex-only for backwards compatibility. */
|
|
10
|
+
export class MatchNodeImpl extends MatchCaseNodeBase {
|
|
11
|
+
caseEditorLabel = 'Cases (regular expressions)';
|
|
12
|
+
caseEditorPlaceholder = 'Case (regular expression)';
|
|
11
13
|
static create() {
|
|
12
|
-
|
|
14
|
+
return {
|
|
13
15
|
type: 'match',
|
|
14
|
-
title:
|
|
16
|
+
title: regexMatchLegacyDisplayName,
|
|
15
17
|
id: nanoid(),
|
|
16
18
|
visualData: {
|
|
17
19
|
x: 0,
|
|
@@ -24,182 +26,27 @@ export class MatchNodeImpl extends NodeImpl {
|
|
|
24
26
|
valueInputMode: 'shared',
|
|
25
27
|
},
|
|
26
28
|
};
|
|
27
|
-
return chartNode;
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
{
|
|
32
|
-
id: 'input',
|
|
33
|
-
title: 'Test',
|
|
34
|
-
dataType: 'string',
|
|
35
|
-
required: true,
|
|
36
|
-
description: 'The value that will be tested against each of the cases.',
|
|
37
|
-
},
|
|
38
|
-
];
|
|
39
|
-
if (this.getValueInputMode() === 'shared') {
|
|
40
|
-
inputs.push({
|
|
41
|
-
id: 'value',
|
|
42
|
-
title: 'Custom value',
|
|
43
|
-
dataType: 'any',
|
|
44
|
-
description: 'The optional value passed through to every matching output. If unconnected, the Test value is passed through.',
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
const portIds = this.getCasePortIds();
|
|
49
|
-
this.data.cases.forEach((caseValue, index) => {
|
|
50
|
-
inputs.push({
|
|
51
|
-
id: this.getCaseValueInputId(portIds[index]),
|
|
52
|
-
title: this.getCaseTitle(caseValue, index),
|
|
53
|
-
dataType: 'any',
|
|
54
|
-
description: `The optional custom value emitted when /${caseValue}/ matches the Test value. If unconnected, Test is passed through.`,
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
inputs.push({
|
|
58
|
-
id: 'value-unmatched',
|
|
59
|
-
title: 'Unmatched',
|
|
60
|
-
dataType: 'any',
|
|
61
|
-
description: 'The optional custom value emitted when no regular expression matches the Test value. If unconnected, Test is passed through.',
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
return inputs;
|
|
30
|
+
matchesCase(input, caseValue) {
|
|
31
|
+
return new RegExp(caseValue).test(input);
|
|
65
32
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const portIds = this.getCasePortIds();
|
|
69
|
-
for (let i = 0; i < this.data.cases.length; i++) {
|
|
70
|
-
outputs.push({
|
|
71
|
-
id: portIds[i],
|
|
72
|
-
title: this.getCaseTitle(this.data.cases[i], i),
|
|
73
|
-
dataType: 'any',
|
|
74
|
-
description: this.getValueInputMode() === 'shared'
|
|
75
|
-
? `The shared Custom value (or Test if Custom value is unconnected) passed through if the test value matches /${this.data.cases[i]}/.`
|
|
76
|
-
: `The corresponding ${this.getCaseTitle(this.data.cases[i], i)} custom value (or Test if it is unconnected) passed through if the test value matches /${this.data.cases[i]}/.`,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
outputs.push({
|
|
80
|
-
id: 'unmatched',
|
|
81
|
-
title: 'Unmatched',
|
|
82
|
-
dataType: 'any',
|
|
83
|
-
description: this.getValueInputMode() === 'shared'
|
|
84
|
-
? 'The shared Custom value (or Test if Custom value is unconnected) passed through if no regexes match.'
|
|
85
|
-
: 'The Unmatched custom value (or Test if it is unconnected) passed through if no regexes match.',
|
|
86
|
-
});
|
|
87
|
-
return outputs;
|
|
33
|
+
describeCaseMatch(caseValue) {
|
|
34
|
+
return `/${caseValue}/ matches the Test value`;
|
|
88
35
|
}
|
|
89
|
-
|
|
90
|
-
return
|
|
91
|
-
}
|
|
92
|
-
getEditors() {
|
|
93
|
-
return [
|
|
94
|
-
{
|
|
95
|
-
type: 'segmented',
|
|
96
|
-
label: 'Matching cases to trigger',
|
|
97
|
-
dataKey: 'exclusive',
|
|
98
|
-
defaultValue: false,
|
|
99
|
-
options: [
|
|
100
|
-
{ value: false, label: 'Trigger all matching cases' },
|
|
101
|
-
{ value: true, label: 'Trigger first only' },
|
|
102
|
-
],
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
type: 'segmented',
|
|
106
|
-
label: 'Custom case values',
|
|
107
|
-
dataKey: 'valueInputMode',
|
|
108
|
-
defaultValue: 'shared',
|
|
109
|
-
options: [
|
|
110
|
-
{ value: 'shared', label: 'One shared custom value' },
|
|
111
|
-
{ value: 'per-output', label: 'Custom values per case' },
|
|
112
|
-
],
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
type: 'stringList',
|
|
116
|
-
dataKey: 'cases',
|
|
117
|
-
label: 'Cases (regular expressions)',
|
|
118
|
-
placeholder: 'Case (regular expression)',
|
|
119
|
-
reorderable: true,
|
|
120
|
-
portBinding: {
|
|
121
|
-
side: 'output',
|
|
122
|
-
identity: 'stored-stable-id',
|
|
123
|
-
idDataKey: 'casePortIds',
|
|
124
|
-
legacyPortIdPattern: {
|
|
125
|
-
kind: 'prefix',
|
|
126
|
-
prefix: 'case',
|
|
127
|
-
startIndex: 1,
|
|
128
|
-
},
|
|
129
|
-
companionBindings: [{ side: 'input', prefix: 'value-' }],
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
];
|
|
36
|
+
describeNoCaseMatches() {
|
|
37
|
+
return 'no regular expression matches the Test value';
|
|
133
38
|
}
|
|
134
39
|
static getUIData() {
|
|
135
40
|
return {
|
|
136
41
|
infoBoxBody: dedent `
|
|
137
|
-
|
|
42
|
+
Legacy regex-only routing node. Existing graphs keep their current behavior.
|
|
43
|
+
|
|
44
|
+
For new graphs, use Match case. It supports exact plain-text cases by default and optional regular-expression matching.
|
|
138
45
|
`,
|
|
139
|
-
infoBoxTitle:
|
|
140
|
-
contextMenuTitle:
|
|
46
|
+
infoBoxTitle: `${regexMatchLegacyDisplayName} Node`,
|
|
47
|
+
contextMenuTitle: regexMatchLegacyDisplayName,
|
|
141
48
|
group: ['Logic'],
|
|
142
49
|
};
|
|
143
50
|
}
|
|
144
|
-
async process(inputs) {
|
|
145
|
-
const inputValue = inputs['input'];
|
|
146
|
-
const inputString = inputValue?.value == null ? undefined : coerceType(inputValue, 'string');
|
|
147
|
-
const portIds = this.getCasePortIds();
|
|
148
|
-
const sharedValue = inputs['value'];
|
|
149
|
-
const getTestOutputValue = () => ({
|
|
150
|
-
type: 'string',
|
|
151
|
-
value: inputString,
|
|
152
|
-
});
|
|
153
|
-
const getSharedOutputValue = () => sharedValue ?? getTestOutputValue();
|
|
154
|
-
const getOutputValue = (portId) => this.getValueInputMode() === 'shared'
|
|
155
|
-
? getSharedOutputValue()
|
|
156
|
-
: inputs[this.getCaseValueInputId(portId)] ?? getTestOutputValue();
|
|
157
|
-
const getUnmatchedOutputValue = () => this.getValueInputMode() === 'shared'
|
|
158
|
-
? getSharedOutputValue()
|
|
159
|
-
: inputs['value-unmatched'] ?? getTestOutputValue();
|
|
160
|
-
const cases = this.data.cases;
|
|
161
|
-
let matched = false;
|
|
162
|
-
const output = {};
|
|
163
|
-
for (let i = 0; i < cases.length; i++) {
|
|
164
|
-
const match = inputString !== undefined && new RegExp(cases[i]).test(inputString);
|
|
165
|
-
const canMatch = !this.data.exclusive || !matched;
|
|
166
|
-
if (match && canMatch) {
|
|
167
|
-
matched = true;
|
|
168
|
-
output[portIds[i]] = getOutputValue(portIds[i]);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
output[portIds[i]] = {
|
|
172
|
-
type: 'control-flow-excluded',
|
|
173
|
-
value: undefined,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
if (!matched) {
|
|
178
|
-
output['unmatched'] = getUnmatchedOutputValue();
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
output['unmatched'] = {
|
|
182
|
-
type: 'control-flow-excluded',
|
|
183
|
-
value: undefined,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return output;
|
|
187
|
-
}
|
|
188
|
-
getValueInputMode() {
|
|
189
|
-
return this.data.valueInputMode === 'per-output' ? 'per-output' : 'shared';
|
|
190
|
-
}
|
|
191
|
-
getCasePortIds() {
|
|
192
|
-
return resolveStoredOrderedPortIds(this.data.cases.length, this.data.casePortIds, {
|
|
193
|
-
kind: 'prefix',
|
|
194
|
-
prefix: 'case',
|
|
195
|
-
startIndex: 1,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
getCaseTitle(caseValue, index) {
|
|
199
|
-
return caseValue?.trim() ? caseValue : `Case ${index + 1}`;
|
|
200
|
-
}
|
|
201
|
-
getCaseValueInputId(portId) {
|
|
202
|
-
return `value-${portId}`;
|
|
203
|
-
}
|
|
204
51
|
}
|
|
205
|
-
export const matchNode = nodeDefinition(MatchNodeImpl,
|
|
52
|
+
export const matchNode = nodeDefinition(MatchNodeImpl, regexMatchLegacyDisplayName);
|
|
@@ -30,6 +30,8 @@ export type StringEditorDefinition<T extends ChartNode> = SharedEditorDefinition
|
|
|
30
30
|
export type ToggleEditorDefinition<T extends ChartNode> = SharedEditorDefinitionProps<T> & {
|
|
31
31
|
type: 'toggle';
|
|
32
32
|
dataKey: DataOfType<T, boolean>;
|
|
33
|
+
/** Value rendered when an older node omits this optional boolean setting. */
|
|
34
|
+
defaultValue?: boolean;
|
|
33
35
|
useInputToggleDataKey?: DataOfType<T, boolean>;
|
|
34
36
|
turnOffDataKeysWhenEnabled?: DataOfType<T, boolean>[];
|
|
35
37
|
};
|