Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit.

  • Loading branch information...
commit a8099493a5508bfbbe5a908c9c5814ae8264e9c4 0 parents
@ericmuyser authored
Showing with 63 additions and 0 deletions.
  1. +23 −0 lib/stringy.js
  2. +25 −0 package.json
  3. +15 −0 readme.md
23 lib/stringy.js
@@ -0,0 +1,23 @@
+function filter(item) {
+ return (function() {
+ var i = 0
+
+ return function(key, value) {
+ if(i !== 0 && typeof(item) === 'object' && typeof(value) == 'object' && item == value)
+ return '[Circular]'
+
+ if(i >= 29) // seems to be a harded maximum of 30 serialized objects?
+ return '[Unknown]'
+
+ ++i // so we know we aren't using the original object anymore
+
+ return value
+ }
+ })(item)
+}
+
+function stringify(item, censor, space) {
+ return JSON.stringify(item, censor ? censor : filter(item), space)
+}
+
+exports.stringify = stringify
25 package.json
@@ -0,0 +1,25 @@
+{
+ "name": "stringy"
+ , "description": "Stringy.js will stringify your circular JS objects without complaint."
+ , "version": "0.1.0"
+ , "homepage": "http://github.com/ericmuyser/stringy"
+ , "repository": {
+ "url": "http://github.com/ericmuyser/stringy"
+ }
+ , "author": "Eric Muyser <eric@muyser.com> (http://eric.muyser.com/)"
+ , "main": "lib/stringy.js"
+ , "engines": {
+ "node": "*"
+ }
+ , "directories" : {
+ "lib" : "./lib"
+ }
+ , "dependencies": {
+ }
+ , "browserify" : {
+ "base" : "./lib"
+ , "main" : "./lib/stringy.js"
+ }
+ , "devDependencies": {
+ }
+}
15 readme.md
@@ -0,0 +1,15 @@
+# Stringy.js
+
+Stringy.js will stringify your circular JS objects [without complaint](http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json/9653082#9653082). Unfortunately it currently fails if the object hierarchy exceeds 30 total items.
+
+## Usage
+
+```js
+var b = {foo: {bar: null}}
+
+b.foo.bar = b // it's circular!
+
+console.log("Filtering: ", b)
+console.log(stringy.stringify(b)) // works!
+console.log(JSON.stringify(b)) // exception
+```
Please sign in to comment.
Something went wrong with that request. Please try again.