Skip to content

Instantly share code, notes, and snippets.

@Mazday21
Created June 1, 2024 10:37
Show Gist options
  • Save Mazday21/cb4ced23837735479e8cc32b09076229 to your computer and use it in GitHub Desktop.
Save Mazday21/cb4ced23837735479e8cc32b09076229 to your computer and use it in GitHub Desktop.
export default function objectToString(obj, indent = '') {
const lines = ['{'];
const entries = Object.entries(obj);
entries.forEach(([key, value], index) => {
if (typeof value === 'object' && value !== null) {
const nestedIndent = `${indent} `;
const nestedLines = objectToString(value, nestedIndent);
lines.push(`${indent} ${key}: ${nestedLines}`);
} else {
lines.push(`${indent} ${key}: ${value}`);
}
if (index === entries.length - 1) {
lines.push(`${indent}}`);
}
});
return lines.join('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment