fast-safe-stringify
Advanced tools
Comparing version 1.1.11 to 1.1.12
module.exports = stringify | ||
stringify.default = stringify | ||
function stringify (obj) { | ||
if (obj && typeof obj.toJSON === 'function') { | ||
obj = obj.toJSON() | ||
if (typeof obj === 'object' && typeof obj.toJSON !== 'function') { | ||
decirc(obj, '', [], null) | ||
} | ||
decirc(obj, '', [], null) | ||
return JSON.stringify(obj) | ||
@@ -30,2 +29,4 @@ } | ||
return | ||
} else if (typeof val.toJSON === 'function') { | ||
return | ||
} else if (parent) { | ||
@@ -32,0 +33,0 @@ if (~stack.indexOf(val)) { |
{ | ||
"name": "fast-safe-stringify", | ||
"version": "1.1.11", | ||
"version": "1.1.12", | ||
"description": "Safely and quickly serialize JavaScript objects", | ||
@@ -16,4 +16,4 @@ "main": "index.js", | ||
"json-stringify-safe": "^5.0.1", | ||
"standard": "^8.0.0", | ||
"tap": "^9.0.0" | ||
"standard": "^9.0.0", | ||
"tap": "^10.0.0" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
31
test.js
@@ -156,1 +156,32 @@ var test = require('tap').test | ||
}) | ||
test('child circular reference with toJSON', function (assert) { | ||
// Create a test object that has an overriden `toJSON` property | ||
TestObject.prototype.toJSON = function () { return { special: 'case' } } | ||
function TestObject (content) {} | ||
// Creating a simple circular object structure | ||
const parentObject = {} | ||
parentObject.childObject = new TestObject() | ||
parentObject.childObject.parentObject = parentObject | ||
// Creating a simple circular object structure | ||
const otherParentObject = new TestObject() | ||
otherParentObject.otherChildObject = {} | ||
otherParentObject.otherChildObject.otherParentObject = otherParentObject | ||
// Making sure our original tests work | ||
assert.deepEqual(parentObject.childObject.parentObject, parentObject) | ||
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject) | ||
// Should both be idempotent | ||
assert.equal(fss(parentObject), '{"childObject":{"special":"case"}}') | ||
assert.equal(fss(otherParentObject), '{"special":"case"}') | ||
// Therefore the following assertion should be `true` | ||
assert.deepEqual(parentObject.childObject.parentObject, parentObject) | ||
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject) | ||
assert.end() | ||
}) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12794
294