Created
September 17, 2017 17:49
-
-
Save autioch/c28910c47b4013a75a23932732fab186 to your computer and use it in GitHub Desktop.
Stringify circular
This file contains hidden or 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
module.exports = function stringifyCircular(obj) { | |
const cache = []; | |
return JSON.stringify(obj, (key, value) => { | |
if (typeof value === 'object' && value !== null) { | |
if (cache.indexOf(value) !== -1) { | |
// Circular reference found, discard key | |
return undefined; | |
} | |
// Store value in our collection | |
cache.push(value); | |
} | |
return value; | |
}, ' '); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment