Skip to content

Commit 58d11aa

Browse files
committed
extend json-node with @Flat command to flatten array
1 parent 9214dc6 commit 58d11aa

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

libs/web-flow-executor/src/nodes/json-utils/transform-json.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,22 @@ describe('transformJSON', () => {
173173
role: 'assistant',
174174
});
175175
});
176+
177+
it('should transform a nested array with @flat', () => {
178+
const input = {
179+
'@flat:output': [1, 2, [3, 4]],
180+
};
181+
const result = transformJSON(input, undefined, 'root', {});
182+
expect((result as any)['output']).toEqual([1, 2, 3, 4]);
183+
});
184+
185+
it('should transform a property from the payload with @flat', () => {
186+
const input = {
187+
'@flat:output': 'test',
188+
};
189+
const result = transformJSON(input, undefined, 'root', {
190+
test: [1, 2, [3, 4]],
191+
});
192+
expect((result as any)['output']).toEqual([1, 2, 3, 4]);
193+
});
176194
});

libs/web-flow-executor/src/nodes/json-utils/transform-json.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ const evaluateAtProperty = (
2323
payload: any,
2424
initialPayload?: any
2525
) => {
26-
if (key.startsWith('@serialize')) {
26+
if (key.startsWith('@flat')) {
27+
const flatProperty = key.replace('@flat:', '');
28+
if (Array.isArray(value)) {
29+
const result = value.flat();
30+
31+
newJson[flatProperty] = result;
32+
} else if (typeof value === 'string') {
33+
const flattenValue = initialPayload[value];
34+
newJson[flatProperty] = flattenValue.flat();
35+
} else {
36+
newJson[flatProperty] = value;
37+
}
38+
} else if (key.startsWith('@serialize')) {
2739
const setProperty = key.replace('@serialize:', '');
2840

2941
const toSerialize: Record<string, any> = {};

0 commit comments

Comments
 (0)