Version of JSON.stringify limitied to a specific depth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const stringifyWithCustomDepth = (obj: any, depth = 1): string => { | |
return !obj | |
? JSON.stringify(obj, null, 2) | |
: typeof obj === 'object' | |
? JSON.stringify( | |
JSON.parse( | |
depth < 1 | |
? '"???"' | |
: `{${Object.keys(obj) | |
.map((k) => `"${k}": ${stringifyWithCustomDepth(obj[k], depth - 1)}`) | |
.join(', ')}}`, | |
), | |
null, | |
2, | |
) | |
: JSON.stringify(obj, null, 2); | |
}; | |
export default stringifyWithCustomDepth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another way to do this
original repo