fast-safe-stringify
Advanced tools
Comparing version 1.1.13 to 1.2.0
@@ -29,3 +29,3 @@ module.exports = stringify | ||
return | ||
} else if (typeof val.toJSON === 'function') { | ||
} else if (typeof val.toJSON === 'function' && !val.toJSON.forceDecirc) { | ||
return | ||
@@ -32,0 +32,0 @@ } else if (parent) { |
{ | ||
"name": "fast-safe-stringify", | ||
"version": "1.1.13", | ||
"version": "1.2.0", | ||
"description": "Safely and quickly serialize JavaScript objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,2 +19,19 @@ # fast-safe-stringify | ||
### toJSON support | ||
`fast-safe-stringify` would not attempt to detect circular dependencies | ||
on objects that have a `toJSON` function. If you need to do that, you | ||
will need to attach a `toJSON.forceDecirc = true` property, like | ||
so: | ||
```js | ||
var obj = { | ||
toJSON: function () { | ||
// something here.. | ||
return { something: 'else' } | ||
} | ||
} | ||
obj.toJSON.forceDecirc = true | ||
``` | ||
## Benchmarks | ||
@@ -21,0 +38,0 @@ |
52
test.js
@@ -187,1 +187,53 @@ var test = require('tap').test | ||
test('null object', function (assert) { | ||
var expected = s(null) | ||
var actual = fss(null) | ||
assert.is(actual, expected) | ||
assert.end() | ||
}) | ||
test('null property', function (assert) { | ||
var expected = s({ f: null }) | ||
var actual = fss({ f: null }) | ||
assert.is(actual, expected) | ||
assert.end() | ||
}) | ||
test('nested child circular reference in toJSON', function (assert) { | ||
var b = {} | ||
var circle = { some: 'data' } | ||
circle.circle = circle | ||
var baz = { circle } | ||
var a = { b, baz } | ||
var o = { | ||
a, | ||
bar: a | ||
} | ||
b.toJSON = function () { | ||
a.b = 2 | ||
return '[Redacted]' | ||
} | ||
baz.toJSON = function () { | ||
a.baz = circle | ||
return '[Redacted]' | ||
} | ||
baz.toJSON.forceDecirc = true | ||
var expected = s({ | ||
a: { | ||
b: '[Redacted]', | ||
baz: '[Redacted]' | ||
}, | ||
bar: { | ||
b: 2, | ||
baz: { | ||
some: 'data', | ||
circle: '[Circular]' | ||
} | ||
} | ||
}) | ||
// fixture.child.actor[Symbol.for('forceDecirc')] = true | ||
var actual = fss(o) | ||
assert.is(actual, expected) | ||
assert.end() | ||
}) |
14215
343
94