Skip to content

Commit

Permalink
implemented decycle for JSON.stringify that should work in IE
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Mar 21, 2013
1 parent 51a4e66 commit 178a8dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
30 changes: 27 additions & 3 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var Stream = require('stream');
var json = typeof JSON === 'object' ? JSON : require('jsonify');
var getSerialize = require('json-stringify-safe').getSerialize()

module.exports = Render;

Expand Down Expand Up @@ -71,8 +70,8 @@ function encodeResult (res, count) {
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';

var ex = json.stringify(res.expected, getSerialize) || '';
var ac = json.stringify(res.actual, getSerialize) || '';
var ex = json.stringify(res.expected, getSerialize()) || '';
var ac = json.stringify(res.actual, getSerialize()) || '';

if (Math.max(ex.length, ac.length) > 65) {
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
Expand All @@ -99,3 +98,28 @@ function encodeResult (res, count) {

return output;
}

function getSerialize() {
var seen = [];

return function (key, value) {
var ret = value;
if (typeof value === 'object' && value) {
var found = false
for (var i = 0; i < seen.length; i++) {
if (seen[i] === value) {
found = true
break;
}
}

if (found) {
ret = '[Circular]'
} else {
seen.push(value)
}
}

return ret
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"dependencies" : {
"jsonify" : "~0.0.0",
"deep-equal" : "~0.0.0",
"defined" : "~0.0.0",
"json-stringify-safe": "~4.0.0"
"defined" : "~0.0.0"
},
"devDependencies" : {
"tap" : "~0.3.0",
Expand Down

0 comments on commit 178a8dc

Please sign in to comment.