Skip to content

Instantly share code, notes, and snippets.

@hisamafahri
Created March 5, 2024 05:23
Show Gist options
  • Save hisamafahri/d7fa0ba3c21e89212afda713744e678c to your computer and use it in GitHub Desktop.
Save hisamafahri/d7fa0ba3c21e89212afda713744e678c to your computer and use it in GitHub Desktop.
axiom like json stringify formatter. https://i.imgur.com/nM3x94w.png
interface CustomObject {
[key: string]: unknown;
}
const formatObject = (obj: CustomObject): JSX.Element => (
<span>
{"{"}
{Object.keys(obj).map((key, index) => (
// eslint-disable-next-line react/no-array-index-key
<span key={index}>
<b>{key}</b>:{" "}
{typeof obj[key] === "object"
? formatObject(obj[key] as CustomObject)
: (obj[key] as string)}
{index < Object.keys(obj).length - 1 && ", "}
</span>
))}
{"}"}
</span>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment