topkat-utils
Version:
A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.
19 lines (16 loc) • 546 B
text/typescript
/** Best way to stringify a value! Default indent: 2 */
export function removeCircularJSONstringify(object: any, indent = 2) {
const getCircularReplacer = () => {
const seen = new WeakSet()
return (_: any, value: any) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return
}
seen.add(value)
}
return value
}
}
return JSON.stringify(object, getCircularReplacer(), indent)
}