2018-12-21 22:18:05 +03:00
|
|
|
import React from 'react';
|
|
|
|
import JSONNestedNode from './JSONNestedNode';
|
2023-01-05 07:17:44 +03:00
|
|
|
import type { CommonInternalProps } from './types';
|
2018-12-21 22:18:05 +03:00
|
|
|
|
|
|
|
// Returns the "n Items" string for this node,
|
|
|
|
// generating and caching it if it hasn't been created yet.
|
2020-08-22 03:13:24 +03:00
|
|
|
function createItemString(data: any, limit: number) {
|
2018-12-21 22:18:05 +03:00
|
|
|
let count = 0;
|
|
|
|
let hasMore = false;
|
|
|
|
if (Number.isSafeInteger(data.size)) {
|
|
|
|
count = data.size;
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for (const entry of data) {
|
|
|
|
if (limit && count + 1 > limit) {
|
|
|
|
hasMore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `${hasMore ? '>' : ''}${count} ${count !== 1 ? 'entries' : 'entry'}`;
|
|
|
|
}
|
|
|
|
|
2023-01-05 07:17:44 +03:00
|
|
|
interface Props extends CommonInternalProps {
|
|
|
|
data: unknown;
|
2020-08-22 03:13:24 +03:00
|
|
|
nodeType: string;
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:18:05 +03:00
|
|
|
// Configures <JSONNestedNode> to render an iterable
|
2023-01-05 07:17:44 +03:00
|
|
|
export default function JSONIterableNode(props: Props) {
|
2018-12-21 22:18:05 +03:00
|
|
|
return (
|
|
|
|
<JSONNestedNode
|
|
|
|
{...props}
|
|
|
|
nodeType="Iterable"
|
|
|
|
nodeTypeIndicator="()"
|
|
|
|
createItemString={createItemString}
|
2023-01-05 07:17:44 +03:00
|
|
|
expandable
|
2018-12-21 22:18:05 +03:00
|
|
|
/>
|
|
|
|
);
|
2023-01-05 07:17:44 +03:00
|
|
|
}
|