Skip to content

Commit

Permalink
Implement refs option (default to true)
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmoxisus committed Dec 1, 2018
1 parent 13a206a commit 8021553
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ exports.decycle = function decycle(object, options, replacer) {
// If the value is an object or array, look to see if we have already
// encountered it. If so, return a $ref/path object.

if (typeof value === 'object' && value !== null) {

This comment has been minimized.

Copy link
@zalmoxisus

zalmoxisus Dec 1, 2018

Author Collaborator

Replacing value !== null here as it's already checked in L95

if (typeof value === 'object' && options['refs']) {
var foundPath = map.get(value);
if (foundPath) {
if (hasCircular && path.indexOf(foundPath) === 0) {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.stringify = function stringify(value, replacer, space, _options) {
'infinity': options
}
}
if (typeof options.refs === 'undefined') options.refs = true;

var decycled = cycle.decycle(value, options, replacer);
if (arguments.length === 1) {
Expand Down
9 changes: 9 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ describe('jsan', function() {
assert.equal(jsan.stringify(obj, null, null, {circular: function() { return '∞!' }}), '{"self":"∞!","a":1,"b":{},"c":{"$jsan":"$.b"}}');
});

it('can use the refs option', function() {
var obj1 = { a: 1 };
var obj = { "prop1": obj1, "prop2": { "prop3": obj1 } };
assert.equal(jsan.stringify(obj, null, null, {refs: true}), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}');
assert.equal(jsan.stringify(obj, null, null, true), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}');
assert.equal(jsan.stringify(obj, null, null, false), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}');
assert.equal(jsan.stringify(obj, null, null, {refs: false}), '{"prop1":{"a":1},"prop2":{"prop3":{"a":1}}}');
});

it('works on objects with "[", "\'", and "]" in the keys', function() {
var obj = {};
obj['["key"]'] = {};
Expand Down

0 comments on commit 8021553

Please sign in to comment.