File size: 793 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { isDefined } from '@/utils';

import {
  type BaseOutputSchemaV2,
  type Leaf,
  type Node,
} from '../types/base-output-schema.type';

export const isFlattenedArrayOutputSchema = (
  schema: BaseOutputSchemaV2 | undefined,
): boolean => {
  if (!isDefined(schema)) {
    return false;
  }

  const keys = Object.keys(schema);

  if (keys.length === 0) {
    return false;
  }

  return keys.every((key, index) => key === String(index));
};

export const getCurrentItemSchemaFromFlattenedArrayOutputSchema = ({
  schema,
  label = 'Current Item',
}: {
  schema: BaseOutputSchemaV2;
  label?: string;
}): Leaf | Node | undefined => {
  const firstItemNode = schema['0'];

  if (!isDefined(firstItemNode)) {
    return undefined;
  }

  return {
    ...firstItemNode,
    label,
  };
};