Skip to content

Commit

Permalink
send dates to JsonPrimitiveValue
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Paul-R committed Sep 30, 2023
1 parent c3e7f4b commit bfb33ae
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
26 changes: 4 additions & 22 deletions src/DataRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function JsonPrimitiveValue({
value,
style,
lastElement
}: JsonRenderProps<string | number | boolean | null | undefined>) {
}: JsonRenderProps<string | number | boolean | Date | null | undefined>) {
let stringValue = value;
let valueStyle = style.otherValue;

Expand All @@ -185,6 +185,8 @@ function JsonPrimitiveValue({
} else if (DataTypeDetection.isBigInt(value)) {
stringValue = `${value.toString()}n`;
valueStyle = style.numberValue;
} else if (DataTypeDetection.isDate(value)) {
stringValue = value.toISOString();
} else {
stringValue = value.toString();
}
Expand All @@ -202,33 +204,13 @@ function JsonPrimitiveValue({
);
}

function DateValue({ field, value, style, lastElement }: JsonRenderProps<Date>) {
const stringValue = value.toISOString();
const valueStyle = style.otherValue;

if (field === '') {
field = '""';
}

return (
<div className={style.basicChildStyle} role='listitem'>
{field && <span className={style.label}>{field}:</span>}
<span className={valueStyle}>{stringValue}</span>
{!lastElement && <span className={style.punctuation}>,</span>}
</div>
);
}

export default function DataRender(props: JsonRenderProps<any>) {
const value = props.value;
if (DataTypeDetection.isArray(value)) {
return <JsonArray {...props} />;
}

if (DataTypeDetection.isObject(value)) {
if (DataTypeDetection.isDate(value)) {
return <DateValue {...props} />;
}
if (DataTypeDetection.isObject(value) && !DataTypeDetection.isDate(value)) {
return <JsonObject {...props} />;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DataTypeDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const isBigInt = (data: any) => {
return typeof data === 'bigint' || data instanceof BigInt;
};

export const isDate = (data: any) => {
export const isDate = (data: unknown): data is Date => {
return !!data && data instanceof Date;
};

Expand Down

0 comments on commit bfb33ae

Please sign in to comment.