Last active
June 5, 2019 13:34
-
-
Save AndersDJohnson/4395147 to your computer and use it in GitHub Desktop.
JSON stringify circular replacer
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
| JSONCircular = (circularValue = '[Circular]') -> | |
| cache = []; | |
| return (key, value) -> | |
| if (typeof value is 'object' && value isnt null) | |
| if (cache.indexOf(value) isnt -1) | |
| return circularValue; | |
| cache.push(value); | |
| return value; | |
| # use: | |
| # JSON.stringify(object, JSONCircular()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment